On Jul 9, 2007, at 5:55 PM, Todd Benson wrote:

> On 7/9/07, Ari Brown <ari / aribrown.com> wrote:
>> But, I take it, I can't modify this single script, right?
>>
>> Start:
>> file.rb
>>
>> so that after I download a new patch, all that's left is:
>> file.rb
>
> Yes, you can do that.
>
> Also, like Diego mentioned about changing behavior of an object when
> loading code ... say I have a file test.rb that looks like this:
>
> class C
>  def f(x)
>    puts x
>  end
> end
>
> and I go into irb ...
>
> irb(main):001:0> load 'test.rb'
> => true
> irb(main):002:0> c = C.new
> => #<C:0x2df909c>
> irb(main):003:0> c.f 1
> 1
> => nil
> irb(main):004:0> c.g 1
> NoMethodError: undefined method 'g' for #<C:0x2df909c>
>            from (irb):4
>
> leaving irb sitting there just the way it is, I modify test.rb by
> adding this method to class C:
>
> def g x
>  puts x + 1
> end
>
> back to irb...
>
> irb(main):005:0> c.g 1
> NoMethodError: undefined method 'g' for #<c:0x2df909c>
>            from (irb):1
> irb(main):006:0> load 'test.rb'  #reloading the file, but _not_  
> creating a new c
> => true
> irb(main):007:0> c.g 1
> 2
> => nil
>
> Todd
>
You can totally do it. It's pretty common. You might want to make a  
temp file while updating, so you can check to make sure the whole  
thing is complete before committing to the new file.
The caveat is just data corruption when objects are in use and if  
they're suddenly gone when you try to access them.
If you think this might happen, it is best to have a script download  
the new file, along with a configuration script that starts when the  
program is restarted. Essentially, you want a hook in your original  
program to check for any updates to run at program start. It's a very  
low cost. Just a quick if statement looking into a data file (or  
looking for one) that says there is or is not an update to apply.  
Multiple 'load's of the same file in irb usually work fine (as an  
example) but occasionally will blow up and make you need to force- 
quit irb.
Example, your code enters a looping mechanism, something is loaded  
that removes or prevents the looping exit condition, voila...  
infinite loop. So, you just need to be careful how you implement the  
update mechanism. Transparent to the user (as much as possible) is  
nice, but no crash/hang/runaway process is better for the user (even  
if they don't know it).

John Joyce