[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Polymorphic Code

Ari Brown

7/8/2007 2:28:00 AM

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.


32 Answers

Stefan Rusterholz

7/8/2007 2:57:00 AM

0

Ari Brown 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/D...

Regards
Stefan

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

David and Sharon Phillips

7/8/2007 3:22:00 AM

0


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

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

7/8/2007 6:57:00 AM

0

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

7/8/2007 8:02:00 AM

0

On 7/8/07, Stefan Rusterholz <apeiros@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/D...
>
> Regards
> Stefan
>
> --
> Posted via http://www.ruby-....
>
>


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

7/8/2007 10:22:00 AM

0


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

David and Sharon Phillips

7/8/2007 10:27:00 AM

0

> 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

dblack

7/8/2007 11:37:00 AM

0

Robert Dober

7/8/2007 2:37:00 PM

0

On 7/8/07, Travis D Warlick Jr <warlickt@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
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Stefan Rusterholz

7/8/2007 3:05:00 PM

0

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

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

Ari Brown

7/8/2007 3:41:00 PM

0


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?

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