Polymorphic Code

On Jul 8, 2007, at 3:58 PM, SonOfLilit wrote:

What kind of modifications are you looking into?

Aur

I was talking about actual code modifications. Could Ruby modify it’s
own code? Take this example…

Ruby asks the user the URL of a code modification thing (eg, a
cleaner version of a patch, or just a patch).

What is the URL?
http://www.awesomesauce.net/awesome.rb
Downloading…
And then Ruby would make modifications to its own code.

Possible or Impossible?

Ari
-------------------------------------------|
Nietzsche is my copilot

Definitely doable. Just use load to load the changes. You would also
have to look at the current object space and make you adjustments. I
believe that changes to a class don’t modify its already existing
instances. But you can always add and remove behavior of an object.

Diego Scataglini

Roseanne Z. wrote:

However, saying that Polymorphism is done at compile-time is completely
wrong.

Another name for Polymorphism is dynamic or late binding or binding at
runtime.

Thanks for the correction. I had to go back to my Programming Languages
book to check myself, and you are quite correct. Not sure what I was
thinking…

On Jul 8, 2007, at 4:56 PM, John J. wrote:

memory_address_X, when it is now at memory_address_y.
Stuff like that. Can be messy business.
On the other hand, it depends what you want to update or modify.
Clearly a lot is possible.
Think of what irb can do.
Luckily ruby will generally just complain about a nil object that
wasn’t expected. so it increases the need for error handling.

Nice!
Is there a suggested library or method to use for that? Or is it just
something I could hack together?

--------------------------------------------|
If you’re not living on the edge,
then you’re just wasting space.

On 7/8/07, Ari B. [email protected] wrote:

I was talking about actual code modifications. Could Ruby modify it’s
own code? Take this example…

Ruby asks the user the URL of a code modification thing (eg, a
cleaner version of a patch, or just a patch).

    What is the URL?
            http://www.awesomesauce.net/awesome.rb
    Downloading....

And then Ruby would make modifications to its own code.

I’m not sure Ruby, the interpreter, can, but a Ruby /script/ can.

Since you can reopen classes, it’s possible to load arbitrary source
code, evaluate it (or just use ‘load’), and then have the changes
reflected.

For example, if your main file was something like this (untested!):

require ‘open-uri’
class X; def foo; “bar”; end; end
puts X.new.foo # => “bar”
puts “What is the URL?”
eval open(gets).read
puts X.new.foo # => “no bar for you”

And some remote Ruby file called “something.rb” was like this:

class X; def foo; “no bar for you”; end; end

And you ran the first file and specified
http://domain.com/something.rb … then, in theory, (and this is all
untested), the functionality of the initial program is changed. You
could then, in theory, save the “patch” to a local file and add it to
a list of “patches” to apply on future executions. Rails’ use of
plugins treads into some of these areas.

This all gets dangerous very quickly though, but I just wanted to
answer the question you asked as I hadn’t seen any answers so far (you
mean polymorphic, as in polymorphic viruses, rather than polymorphism,
it seems).

Cheers,
Peter C.

On 7/9/07, Ari B. [email protected] 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

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

On Jul 8, 2007, at 5:31 PM, diego scataglini wrote:

Definitely doable. Just use load to load the changes. You would
also have to look at the current object space and make you
adjustments. I believe that changes to a class don’t modify its
already existing instances. But you can always add and remove
behavior of an object.

Diego Scataglini

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.

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

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 J.

here you’ll find good information about polymorphism in ruby:
http://vx.netlux.org/lib/vsp20.html

but treat carefully :wink:


greets

                 one must still have chaos in oneself to be able to

give birth to a dancing star

On Tue, 10 Jul 2007 00:00:21 +0900, Travis D Warlick Jr wrote:

Roseanne Z. wrote:

However, saying that Polymorphism is done at compile-time is
completely wrong.

Another name for Polymorphism is dynamic or late binding or
binding at runtime.

Thanks for the correction. I had to go back to my Programming
Languages book to check myself, and you are quite correct. Not
sure what I was thinking…

Ok, so since you’ve just been back to the book, please explain what
the term means in non-geek language!


Chris G.

“If everything seems under control, you’re
just not going fast enough.” – Mario Andretti

On Jul 8, 3:31 pm, diego scataglini [email protected] wrote:

I believe that changes to a class don’t modify its already existing
instances. But you can always add and remove behavior of an object.

C:>irb
irb(main):001:0> class Foo; def sq(x) x*x end; end
irb(main):002:0> f = Foo.new
irb(main):003:0> f.sq( 12 )
=> 144

irb(main):004:0> class Foo; def double(x) x+x end; end
irb(main):005:0> f.double( 21 )
=> 42

irb(main):006:0> class Foo; def sq(x) xxx end; end
irb(main):007:0> f.sq( 12 )
=> 1728

Thank you for the correction =)

Diego Scataglini

haha, I’ve read it before. Excellent page, and it was exactly what I
was looking for! Thanks though.

On Jul 11, 2007, at 9:18 AM, anansi wrote:

~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.