Polymorphic Code


#1

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.


#2

Ari B. wrote:

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.

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


#3

On 08/07/2007, at 12:28 PM, Ari B. 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.

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


#4

Stefan R. wrote:

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


#5

On 7/8/07, Stefan R. removed_email_address@domain.invalid 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 :wink: are related.

Cheers
Robert


#6

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


#7

Hi –

On Sun, 8 Jul 2007, Sharon P. 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?
:slight_smile:

David


#8

On Jul 08, 2007, at 07:37 , removed_email_address@domain.invalid wrote:

:slight_smile:
+1 for Sharon :slight_smile:


#9

On 7/8/07, Travis D Warlick Jr removed_email_address@domain.invalid 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


#10

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
in " 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


#11

On Jul 8, 2007, at 10:41 AM, Ari B. 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.


#12

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

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

+1 for Ruby gender bending method


#13

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

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

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

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.


#14

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.


#15

On Jul 7, 2007, at 11:21 PM, Sharon P. 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.

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

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


#16

Sharon P. wrote:

On 08/07/2007, at 12:28 PM, Ari B. 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


#17

On 7/8/07, Stefan R. removed_email_address@domain.invalid wrote:

Seems I have to clear things a bit up, as I got the feeling I’m
misunderstood.
Aren’t we all :wink:
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
:wink: 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


#18

What kind of modifications are you looking into?

Aur


#19

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
#=================


#20

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

it’s own code? Take this example…

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.