Polymorphic Code

J

John Joyce

<snip>

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

You mean like a Software Update system? yes.
Lots of software does it.
The only thing is that some parts of the program can't be safely
modified at runtime.
that's why some system or application software updates/patches
require you to restart your computer.
The risky part is overstepping bounds. If one application or the
system is using something, and you alter it, it is often possible to
have the original process believing some_thing is at
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.
 
P

Peter Cooper

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 Cooper
http://www.rubyinside.com/
 
D

diego scataglini

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
 
T

Travis D Warlick Jr

Roseanne said:
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...
 
A

Ari Brown

On Jul 8, 2007, at 4:56 PM, John Joyce wrote:
You mean like a Software Update system? yes.
Lots of software does it.
The only thing is that some parts of the program can't be safely
modified at runtime.
that's why some system or application software updates/patches
require you to restart your computer.
The risky part is overstepping bounds. If one application or the
system is using something, and you alter it, it is often possible
to have the original process believing some_thing is at
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.
 
A

Ari Brown

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


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

Phrogz

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) x*x*x end; end
irb(main):007:0> f.sq( 12 )
=> 1728
 
T

Todd Benson

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
 
J

John Joyce

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
 
A

Ari Brown

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

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

but treat carefully ;)

--
greets

one must still have chaos in oneself to be able
to give birth to a dancing star

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Clearing the RAM 7
SCP Ruby 2
Alternate Regular Expressions? 25
AVL Tree 4
Rubinius on Mac PPC 3
Keylogging in Ruby 0
[ANN] fire 1.2.0 1
LSRC Name Picker 4

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top