Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Polymorphic Code

Reply
Thread Tools

Polymorphic Code

 
 
anansi
Guest
Posts: n/a
 
      07-08-2007
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
 
Reply With Quote
 
 
 
 
John Joyce
Guest
Posts: n/a
 
      07-08-2007

On Jul 8, 2007, at 3:31 PM, Ari Brown wrote:

>
> On Jul 8, 2007, at 3:58 PM, SonOfLilit wrote:
>
>> What kind of modifications are you looking into?
>>
>>
>> Aur

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

 
Reply With Quote
 
 
 
 
Peter Cooper
Guest
Posts: n/a
 
      07-08-2007
On 7/8/07, Ari Brown <(E-Mail Removed)> 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 Cooper
http://www.rubyinside.com/

 
Reply With Quote
 
diego scataglini
Guest
Posts: n/a
 
      07-08-2007
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

On Jul 8, 2007, at 4:31 PM, Ari Brown <(E-Mail Removed)> wrote:

>
> On Jul 8, 2007, at 3:58 PM, SonOfLilit wrote:
>
>> What kind of modifications are you looking into?
>>
>>
>> Aur

> <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
> -------------------------------------------|
> Nietzsche is my copilot
>
>
>


 
Reply With Quote
 
Travis D Warlick Jr
Guest
Posts: n/a
 
      07-09-2007
Roseanne Zhang 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...

--
Travis

 
Reply With Quote
 
Ari Brown
Guest
Posts: n/a
 
      07-09-2007

On Jul 8, 2007, at 4:56 PM, John Joyce wrote:
<snip>
>
> 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.



 
Reply With Quote
 
Ari Brown
Guest
Posts: n/a
 
      07-09-2007
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.


 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      07-09-2007
On Jul 8, 3:31 pm, diego scataglini <(E-Mail Removed)> 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) x*x*x end; end
irb(main):007:0> f.sq( 12 )
=> 1728

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      07-09-2007
On 7/9/07, Ari Brown <(E-Mail Removed)> 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

 
Reply With Quote
 
John Joyce
Guest
Posts: n/a
 
      07-10-2007

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

> On 7/9/07, Ari Brown <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Polymorphic data type andy.wagg@gb.schneider-electric.com XML 2 02-19-2005 03:51 PM
SOAP: Creating a polymorphic Data Type andy.wagg@gb.schneider-electric.com Java 0 02-17-2005 01:54 PM
can webmethods be polymorphic? Manco ASP .Net 1 02-03-2005 08:59 AM
polymorphic question Khanh Le Java 3 05-02-2004 01:50 PM
polymorphic behaviour from class constant Thomas Britton Java 1 05-02-2004 10:36 AM



Advertisments