A
anansi
here you'll find good information about polymorphism in ruby:
http://vx.netlux.org/lib/vsp20.html
but treat carefully
http://vx.netlux.org/lib/vsp20.html
but treat carefully
<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
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.
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.
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.
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
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.
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
You can totally do it. It's pretty common. You might want to make aYes, 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
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
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.