Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Polymorphic Code

26 views
Skip to first unread message

Ari Brown

unread,
Jul 7, 2007, 10:28:21 PM7/7/07
to
Hey,
Just a curious question.

So does ruby have anything to accommodate for it? If not, what about
a work around?

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


Stefan Rusterholz

unread,
Jul 7, 2007, 10:56:45 PM7/7/07
to

For your own good, don't do that. Don't work your way around how a
language works to simulate some patterns you learned in another
language. That just leads to bad code and wasted time (no need to learn
a new language if you just continue to code in the other language).

For the ruby way of that, you may want to take a look at
http://en.wikipedia.org/wiki/Duck_typing

Regards
Stefan

--
Posted via http://www.ruby-forum.com/.

Sharon Phillips

unread,
Jul 7, 2007, 11:21:59 PM7/7/07
to

Do you mean something like this (example below)?
What you should be aware of is that Ruby doesn't require you to cast
objects to a particular type in order to call a method. You may have
a number of objects of completely different classes in your
collection, and as long as they all respond to the method you're
interested in then you can iterate through and call that method (duck
typing). This makes interfaces redundant and is a fantastically
useful feature.

Cheers,
Dave

class Animal
attr_reader :name

def initialize(name)
@name= name
end

def noise
"some strange grunty sound"
end

end

class Dog < Animal
def noise
"Woof!"
end
end

class Cat < Animal
def noise
"Meow"
end
end

animals= [Dog.new("Fido"), Cat.new("Socks"), Animal.new("Suzi")]
animals.each do |animal|
puts "#{animal.name} says #{animal.noise}"
end

>

Fido says Woof!
Socks says Meow
Suzi says some strange grunty sound

Robert Klemme

unread,
Jul 8, 2007, 2:56:59 AM7/8/07
to
On 08.07.2007 04:28, Ari Brown wrote:
> So does ruby have anything to accommodate for it? If not, what about a
> work around?

All method calls are virtual so yes, polymorphism is built right into
the language.

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

I am not sure that comparison holds: A pseudo random number generator
follows strict rules. Ah, never mind...

Kind regards

robert

Robert Dober

unread,
Jul 8, 2007, 4:02:24 AM7/8/07
to
On 7/8/07, Stefan Rusterholz <ape...@gmx.net> wrote:

> For your own good, don't do that. Don't work your way around how a
> language works to simulate some patterns you learned in another
> language. That just leads to bad code and wasted time (no need to learn
> a new language if you just continue to code in the other language).

Stefan, thanks for defending the ducks ;). But I feel that you forget
that Ruby is perfectly polymorphic as Sharon has shown above. I do not
really see how DT and Polymurphy ;) are related.

Cheers
Robert


>
> For the ruby way of that, you may want to take a look at
> http://en.wikipedia.org/wiki/Duck_typing
>
> Regards
> Stefan
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Travis D Warlick Jr

unread,
Jul 8, 2007, 6:21:57 AM7/8/07
to

Stefan Rusterholz wrote:
> Ari Brown wrote:
>> Hey,
>> Just a curious question.
>>
>> So does ruby have anything to accommodate for it? If not, what about
>> a work around?
>
> For your own good, don't do that. Don't work your way around how a
> language works to simulate some patterns you learned in another
> language. That just leads to bad code and wasted time (no need to learn
> a new language if you just continue to code in the other language).

The difference between Polymorphism and Dynamic-Typing is essentially
that the former is done at compile-time and the latter at runtime. The
similarity between them; however, is that they more-or-less do the same
thing.

So, to be technical, Ruby is _not_ a Polymorphic language. That being
said, Dynamic Typing make Ruby act Polymorphic.

--
*************************************
* Travis D Warlick, Jr
* Lead Developer
* Operis Systems, LLC
*************************************

Sharon Phillips

unread,
Jul 8, 2007, 6:27:08 AM7/8/07
to
> that Ruby is perfectly polymorphic as Sharon has shown above.

Thanks Robert, except I'm Dave. I use my wife's email which seems to
confuse things (long story).

Cheers,
Dave

dbl...@wobblini.net

unread,
Jul 8, 2007, 7:37:10 AM7/8/07
to
Hi --

Awww, we have so many Dav(e|id)s already. Can't we call you Sharon?
:-)


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Robert Dober

unread,
Jul 8, 2007, 10:36:31 AM7/8/07
to
On 7/8/07, Travis D Warlick Jr <warl...@operissystems.com> wrote:
>

> The difference between Polymorphism and Dynamic-Typing is essentially
> that the former is done at compile-time and the latter at runtime. The
> similarity between them; however, is that they more-or-less do the same
> thing.

In that context Stefan's response would indeed make some sense, I do
however not adhere to the differentiation.
Polymorphic behavior seems completely unrelated to implementation, it
is IMHO a dangerous path to walk, to define a language by it's
implementation details.


>
> So, to be technical, Ruby is _not_ a Polymorphic language. That being
> said, Dynamic Typing make Ruby act Polymorphic.
>

Robert

Stefan Rusterholz

unread,
Jul 8, 2007, 11:04:31 AM7/8/07
to
Seems I have to clear things a bit up, as I got the feeling I'm
misunderstood.
I don't say ruby doesn't have X or Y or so. I say asking "How do I do
<Pattern A known from language X> in <language Y>" is the wrong
approach.
That way you end up asking (contrieved example ahead) how to do a for
loop in ruby and in turn iterate over e.g. an array using some odd
construct intended to simulate a for loop which doesn't exist 1:1 in
ruby instead of just using the way nicer each.
Instead IMHO you should ask "How do I solve problem X?"
As in "how do I iterate over an array?"

I'm hope I'm clearer this time.

Ari Brown

unread,
Jul 8, 2007, 11:41:16 AM7/8/07
to

On Jul 7, 2007, at 11:21 PM, Sharon Phillips wrote:
> Do you mean something like this (example below)?
> What you should be aware of is that Ruby doesn't require you to
> cast objects to a particular type in order to call a method. You
> may have a number of objects of completely different classes in
> your collection, and as long as they all respond to the method
> you're interested in then you can iterate through and call that
> method (duck typing). This makes interfaces redundant and is a
> fantastically useful feature.
<snip>

Not quite. What I mean is is there a way to make Ruby actually modify
the code?

Lyle Johnson

unread,
Jul 8, 2007, 12:07:16 PM7/8/07
to

On Jul 8, 2007, at 10:41 AM, Ari Brown wrote:

> Not quite. What I mean is is there a way to make Ruby actually
> modify the code?

Looks like this person has looked into it:

http://vx.netlux.org/lib/vsp20.html

I also know that various people have looked into Ruby code
obfuscation; try Googling for "ruby obfuscation". I don't think
there's any language feature that's specifically intended to support
the idea, though.

John Joyce

unread,
Jul 8, 2007, 12:15:37 PM7/8/07
to

On Jul 8, 2007, at 7:33 AM, Wayne E. Seguin wrote:

> On Jul 08, 2007, at 07:37 , dbl...@wobblini.net wrote:
>> Hi --
>>
>> On Sun, 8 Jul 2007, Sharon Phillips wrote:
>>
>>>> that Ruby is perfectly polymorphic as Sharon has shown above.
>>>
>>> Thanks Robert, except I'm Dave. I use my wife's email which seems
>>> to confuse things (long story).
>>
>> Awww, we have so many Dav(e|id)s already. Can't we call you Sharon?
>> :-)
>

> +1 for Sharon :)
>
> --
> Wayne E. Seguin
> Sr. Systems Architect & Systems Admin
> wayne...@gmail.com
>
>
>
+1 for Ruby gender bending method


John Joyce

unread,
Jul 8, 2007, 12:24:43 PM7/8/07
to

On Jul 8, 2007, at 10:41 AM, Ari Brown wrote:

Yes and no. Certainly, you could write code that dynamically writes/
configures other code files as things occur but it's kind of
pointless in most cases. The same effect can be achieved through
branching and looping. Perhaps you should read some about A.I.
Arificial Intelligence and fuzzy decision making is kind of another
aspect of program control. Branching, looping, and LEARNING. Machine
learning exists, but the kind where we train it by showing it
examples that it sees as patterns of yes or no and builds a
heuristic. Making a machine learn on its own through independent
discovery is something different.

Roseanne Zhang

unread,
Jul 8, 2007, 12:26:09 PM7/8/07
to
Travis D Warlick Jr wrote:
> The difference between Polymorphism and Dynamic-Typing is essentially
> that the former is done at compile-time and the latter at runtime. The
> similarity between them; however, is that they more-or-less do the same
> thing.
>

I agree they are different.

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.

Roseanne Zhang

unread,
Jul 8, 2007, 12:37:44 PM7/8/07
to
Sharon Phillips wrote:
> On 08/07/2007, at 12:28 PM, Ari Brown wrote:

Modified Sharon :)'s code, and make it more "like" polymorphism

class Animal
attr_reader :name
def initialize(name)
@name= name
end

def says
@name + " says some strange grunty sound"
end
end

class Dog < Animal
def says
@name + " says Woof!"
end
end

class Cat < Animal
def says
@name + " says Meow"
end
end

animal = Dog.new("Fido")
puts animal.says
animal = Cat.new("Socks")
puts animal.says
animal = Animal.new("Suzi")
puts animal.says

Roseanne Zhang

unread,
Jul 8, 2007, 1:32:13 PM7/8/07
to
To show polymorphism and duck-typing are 2 different animals :), I add
more code and comment in the above example. Pay attention to the Radio.
#=================

class Animal
attr_reader :name
def initialize(name)
@name= name
end
def says
@name + " says some strange grunty sound"
end
end

class Dog < Animal
def says
@name + " says Woof!"
end
end

class Cat < Animal
attr_reader :name
def says
@name + " says News"
end
end

# Attention: Radio is not an Animal
class Radio


attr_reader :name
def initialize(name)
@name= name
end
def says

@name + " says News"
end
end

animal = Dog.new("Fido")
puts animal.says
animal = Cat.new("Socks")
puts animal.says
animal = Animal.new("Suzi")
puts animal.says

# here animal is not an Animal any more!!!
# It will not compile in C++/Java
# It is fine, since ruby duck/dynamic typing
animal = Radio.new("BBC")
puts animal.says
#=================

Robert Dober

unread,
Jul 8, 2007, 3:41:12 PM7/8/07
to
On 7/8/07, Stefan Rusterholz <ape...@gmx.net> wrote:
> Seems I have to clear things a bit up, as I got the feeling I'm
> misunderstood.
Aren't we all ;)
Hmm I gotta go code hunt in the libraries.

I use Polymorphism extensively, and I use Duck Typing extensively in
the same Framework. I have Firewall Rules, they are highly polymorphic
-- and I was thinking to replace the polymorphism by delegation
already because it might scale better, I use Duck Typing in a
completely different angle of the application; My DT objects are
servers.
Polymorphism could be used too -- I think I understand you better now
;) but that would not make lot's of sense as the protocol is tiny (#<<
actually).
The protocol I am using in my rules is huge (~30methods) so the
classical approach makes some sense (still I am a Zero on Delegation
and might miss some opportunities in that corner) as I inherit a lot
and relations like TCPForwarder includes Forwarder, includes TCPRule
etc. make some sense.

Maybe one is entitled to say Ruby offers more as the classical OO
approach, think twice before using it, I might agree.

But for the time being I still insist that Ruby support PM natively,
it would be unfair to deny it.

Cheers
Robert


> I don't say ruby doesn't have X or Y or so. I say asking "How do I do
> <Pattern A known from language X> in <language Y>" is the wrong
> approach.
> That way you end up asking (contrieved example ahead) how to do a for
> loop in ruby and in turn iterate over e.g. an array using some odd
> construct intended to simulate a for loop which doesn't exist 1:1 in
> ruby instead of just using the way nicer each.
> Instead IMHO you should ask "How do I solve problem X?"
> As in "how do I iterate over an array?"
>
> I'm hope I'm clearer this time.
> Regards
> Stefan
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

SonOfLilit

unread,
Jul 8, 2007, 3:58:58 PM7/8/07
to
What kind of modifications are you looking into?


Aur

Ari Brown

unread,
Jul 8, 2007, 4:31:37 PM7/8/07
to

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

anansi

unread,
Jul 8, 2007, 4:46:15 PM7/8/07
to
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

John Joyce

unread,
Jul 8, 2007, 4:56:36 PM7/8/07
to

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.

Peter Cooper

unread,
Jul 8, 2007, 5:19:30 PM7/8/07
to
On 7/8/07, Ari Brown <a...@aribrown.com> 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/

diego scataglini

unread,
Jul 8, 2007, 5:31:58 PM7/8/07
to
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

Travis D Warlick Jr

unread,
Jul 9, 2007, 11:00:21 AM7/9/07
to
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

Chris Game

unread,
Jul 9, 2007, 1:47:08 PM7/9/07
to

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

--
Chris Game

"If everything seems under control, you're
just not going fast enough." -- Mario Andretti

Ari Brown

unread,
Jul 9, 2007, 6:14:57 PM7/9/07
to

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.

Ari Brown

unread,
Jul 9, 2007, 6:21:37 PM7/9/07
to
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

Phrogz

unread,
Jul 9, 2007, 6:31:49 PM7/9/07
to
On Jul 8, 3:31 pm, diego scataglini <dwebsub...@gmail.com> 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

Todd Benson

unread,
Jul 9, 2007, 6:55:54 PM7/9/07
to
On 7/9/07, Ari Brown <a...@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

John Joyce

unread,
Jul 10, 2007, 9:41:35 AM7/10/07
to

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

diego scataglini

unread,
Jul 11, 2007, 9:57:01 AM7/11/07
to
Thank you for the correction =)

Diego Scataglini

Ari Brown

unread,
Jul 11, 2007, 3:15:49 PM7/11/07
to
haha, I've read it before. Excellent page, and it was exactly what I
was looking for! Thanks though.

~ Ari

0 new messages