Google Groups Home
Help | Sign in
Polymorphic Code
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 33 - Collapse all   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Ari Brown  
View profile
 More options Jul 7 2007, 10:28 pm
Newsgroups: comp.lang.ruby
From: Ari Brown <a...@aribrown.com>
Date: Sun, 8 Jul 2007 11:28:21 +0900
Local: Sat, Jul 7 2007 10:28 pm
Subject: Polymorphic Code
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan Rusterholz  
View profile
 More options Jul 7 2007, 10:56 pm
Newsgroups: comp.lang.ruby
From: Stefan Rusterholz <apei...@gmx.net>
Date: Sun, 8 Jul 2007 11:56:45 +0900
Local: Sat, Jul 7 2007 10:56 pm
Subject: Re: Polymorphic Code

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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sharon Phillips  
View profile
 More options Jul 7 2007, 11:21 pm
Newsgroups: comp.lang.ruby
From: Sharon Phillips <phillip...@yahoo.co.uk>
Date: Sun, 8 Jul 2007 12:21:59 +0900
Local: Sat, Jul 7 2007 11:21 pm
Subject: Re: Polymorphic Code

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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile
 More options Jul 8 2007, 2:56 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Sun, 08 Jul 2007 08:56:59 +0200
Local: Sun, Jul 8 2007 2:56 am
Subject: Re: Polymorphic Code
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile
 More options Jul 8 2007, 4:02 am
Newsgroups: comp.lang.ruby
From: "Robert Dober" <robert.do...@gmail.com>
Date: Sun, 8 Jul 2007 17:02:24 +0900
Local: Sun, Jul 8 2007 4:02 am
Subject: Re: Polymorphic Code
On 7/8/07, Stefan Rusterholz <apei...@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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Travis D Warlick Jr  
View profile
 More options Jul 8 2007, 6:21 am
Newsgroups: comp.lang.ruby
From: Travis D Warlick Jr <warli...@operissystems.com>
Date: Sun, 8 Jul 2007 19:21:57 +0900
Local: Sun, Jul 8 2007 6:21 am
Subject: Re: Polymorphic Code

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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sharon Phillips  
View profile
 More options Jul 8 2007, 6:27 am
Newsgroups: comp.lang.ruby
From: Sharon Phillips <phillip...@yahoo.co.uk>
Date: Sun, 8 Jul 2007 19:27:08 +0900
Local: Sun, Jul 8 2007 6:27 am
Subject: Re: Polymorphic Code

> 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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dbl...@wobblini.net  
View profile
 More options Jul 8 2007, 7:37 am
Newsgroups: comp.lang.ruby
From: dbl...@wobblini.net
Date: Sun, 8 Jul 2007 20:37:10 +0900
Local: Sun, Jul 8 2007 7:37 am
Subject: Re: Polymorphic Code
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Dober  
View profile
 More options Jul 8 2007, 10:36 am
Newsgroups: comp.lang.ruby
From: "Robert Dober" <robert.do...@gmail.com>
Date: Sun, 8 Jul 2007 23:36:31 +0900
Local: Sun, Jul 8 2007 10:36 am
Subject: Re: Polymorphic Code
On 7/8/07, Travis D Warlick Jr <warli...@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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan Rusterholz  
View profile
 More options Jul 8 2007, 11:04 am
Newsgroups: comp.lang.ruby
From: Stefan Rusterholz <apei...@gmx.net>
Date: Mon, 9 Jul 2007 00:04:31 +0900
Local: Sun, Jul 8 2007 11:04 am
Subject: Re: Polymorphic Code
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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ari Brown  
View profile
 More options Jul 8 2007, 11:41 am
Newsgroups: comp.lang.ruby
From: Ari Brown <a...@aribrown.com>
Date: Mon, 9 Jul 2007 00:41:16 +0900
Local: Sun, Jul 8 2007 11:41 am
Subject: Re: Polymorphic Code

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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lyle Johnson  
View profile
 More options Jul 8 2007, 12:07 pm
Newsgroups: comp.lang.ruby
From: Lyle Johnson <lyle.john...@gmail.com>
Date: Mon, 9 Jul 2007 01:07:16 +0900
Local: Sun, Jul 8 2007 12:07 pm
Subject: Re: Polymorphic Code

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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Joyce  
View profile
 More options Jul 8 2007, 12:15 pm
Newsgroups: comp.lang.ruby
From: John Joyce <dangerwillrobinsondan...@gmail.com>
Date: Mon, 9 Jul 2007 01:15:37 +0900
Local: Sun, Jul 8 2007 12:15 pm
Subject: Re: Polymorphic Code

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