Polymorphic Code

Discussion in 'Ruby' started by Ari Brown, Jul 8, 2007.

  1. Ari Brown

    Ari Brown Guest

    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.
    Ari Brown, Jul 8, 2007
    #1
    1. Advertising

  2. 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/.
    Stefan Rusterholz, Jul 8, 2007
    #2
    1. Advertising

  3. 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
    Sharon Phillips, Jul 8, 2007
    #3
  4. 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 Klemme, Jul 8, 2007
    #4
  5. Ari Brown

    Robert Dober Guest

    On 7/8/07, Stefan Rusterholz <> 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
    Robert Dober, Jul 8, 2007
    #5
  6. 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
    *************************************
    Travis D Warlick Jr, Jul 8, 2007
    #6
  7. > 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
    Sharon Phillips, Jul 8, 2007
    #7
  8. Ari Brown

    Guest

    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)
    , Jul 8, 2007
    #8
  9. Ari Brown

    Robert Dober Guest

    On 7/8/07, 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.


    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
    Robert Dober, Jul 8, 2007
    #9
  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
    <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/.
    Stefan Rusterholz, Jul 8, 2007
    #10
  11. Ari Brown

    Ari Brown Guest

    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.
    Ari Brown, Jul 8, 2007
    #11
  12. Ari Brown

    Lyle Johnson Guest

    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.
    Lyle Johnson, Jul 8, 2007
    #12
  13. Ari Brown

    John Joyce Guest

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

    > On Jul 08, 2007, at 07:37 , wrote:
    >> 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?
    >> :)

    >
    > +1 for Sharon :)
    >
    > --
    > Wayne E. Seguin
    > Sr. Systems Architect & Systems Admin
    >
    >
    >
    >

    +1 for Ruby gender bending method
    John Joyce, Jul 8, 2007
    #13
  14. Ari Brown

    John Joyce Guest

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

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

    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.
    John Joyce, Jul 8, 2007
    #14
  15. 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.

    --
    Posted via http://www.ruby-forum.com/.
    Roseanne Zhang, Jul 8, 2007
    #15
  16. Sharon Phillips wrote:
    > 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


    --
    Posted via http://www.ruby-forum.com/.
    Roseanne Zhang, Jul 8, 2007
    #16
  17. 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
    #=================

    --
    Posted via http://www.ruby-forum.com/.
    Roseanne Zhang, Jul 8, 2007
    #17
  18. Ari Brown

    Robert Dober Guest

    On 7/8/07, Stefan Rusterholz <> wrote:
    > 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
    > 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/.
    >
    >



    --
    I always knew that one day Smalltalk would replace Java.
    I just didn't know it would be called Ruby
    -- Kent Beck
    Robert Dober, Jul 8, 2007
    #18
  19. Ari Brown

    SonOfLilit Guest

    What kind of modifications are you looking into?


    Aur

    On 7/8/07, Ari Brown <> wrote:
    >
    > 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.
    >
    >
    >
    SonOfLilit, Jul 8, 2007
    #19
  20. Ari Brown

    Ari Brown Guest

    On Jul 8, 2007, at 3:58 PM, SonOfLilit wrote:

    > 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
    Ari Brown, Jul 8, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manco

    can webmethods be polymorphic?

    Manco, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    315
    Lionel LASKE
    Feb 3, 2005
  2. Thomas Britton

    polymorphic behaviour from class constant

    Thomas Britton, May 1, 2004, in forum: Java
    Replies:
    1
    Views:
    325
    Chris Uppal
    May 2, 2004
  3. Khanh  Le

    polymorphic question

    Khanh Le, May 2, 2004, in forum: Java
    Replies:
    3
    Views:
    384
    Tim Van Wassenhove
    May 2, 2004
  4. -electric.com

    SOAP: Creating a polymorphic Data Type

    -electric.com, Feb 17, 2005, in forum: Java
    Replies:
    0
    Views:
    379
    -electric.com
    Feb 17, 2005
  5. -electric.com

    Polymorphic data type

    -electric.com, Feb 18, 2005, in forum: XML
    Replies:
    2
    Views:
    716
Loading...

Share This Page