Polymorphic Code

A

Ari Brown

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

Stefan Rusterholz

Ari said:
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
 
S

Sharon Phillips

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.

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
 
R

Robert Klemme

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
 
R

Robert Dober

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
 
T

Travis D Warlick Jr

Stefan said:
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
*************************************
 
S

Sharon Phillips

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
 
R

Robert Dober

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
 
S

Stefan Rusterholz

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.
Regards
Stefan
 
A

Ari Brown

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?

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

Lyle Johnson

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

John Joyce

<snip>

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

Roseanne Zhang

Travis said:
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.
 
R

Roseanne Zhang

Sharon said:
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
 
R

Roseanne Zhang

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

Robert Dober

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
 
A

Ari Brown

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
 

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

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top