Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Polymorphic Code

Reply
Thread Tools

Polymorphic Code

 
 
Ari Brown
Guest
Posts: n/a
 
      07-08-2007
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.


 
Reply With Quote
 
 
 
 
Stefan Rusterholz
Guest
Posts: n/a
 
      07-08-2007
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/Duck_typing

Regards
Stefan

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

 
Reply With Quote
 
 
 
 
Sharon Phillips
Guest
Posts: n/a
 
      07-08-2007

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



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-08-2007
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
 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-08-2007
On 7/8/07, Stefan Rusterholz <(E-Mail Removed)> 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

 
Reply With Quote
 
Travis D Warlick Jr
Guest
Posts: n/a
 
      07-08-2007

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

 
Reply With Quote
 
Sharon Phillips
Guest
Posts: n/a
 
      07-08-2007
> 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

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      07-08-2007
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?



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)

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-08-2007
On 7/8/07, Travis D Warlick Jr <(E-Mail Removed)> 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

 
Reply With Quote
 
Stefan Rusterholz
Guest
Posts: n/a
 
      07-08-2007
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Polymorphic data type andy.wagg@gb.schneider-electric.com XML 2 02-19-2005 03:51 PM
SOAP: Creating a polymorphic Data Type andy.wagg@gb.schneider-electric.com Java 0 02-17-2005 01:54 PM
can webmethods be polymorphic? Manco ASP .Net 1 02-03-2005 08:59 AM
polymorphic question Khanh Le Java 3 05-02-2004 01:50 PM
polymorphic behaviour from class constant Thomas Britton Java 1 05-02-2004 10:36 AM



Advertisments