Duck-typing

Discussion in 'Ruby' started by Daniel Schierbeck, Oct 18, 2005.

  1. Tell me if this is stupid. I'm sure there's a simpler and faster way to
    do this, but I'm interested in knowing whether the idea itself is...
    well, stupid.

    class Object
    def quacks_like? (klass)
    klass.public_instance_methods.each do |method|
    return false unless respond_to? method
    end
    return true
    end
    end

    class A
    def foo; end
    def bar; end
    end

    class B
    def foo; end
    end

    if A.new.quacks_like? B
    puts "A quacks like B"
    else
    puts "A doesn't quack like B"
    end

    # => "A quacks like B"

    if B.new.quacks_like? A
    puts "B quacks like A"
    else
    puts "B doesn't quack like A"
    end

    # => "A doesn't quack like B"

    Basically, when I call obj.quacks_like? Klass I ask if every public
    method defined in Klass is defined in obj as well.


    Cheers,
    Daniel
    Daniel Schierbeck, Oct 18, 2005
    #1
    1. Advertising

  2. On 10/18/05, Daniel Schierbeck <> wrote:
    > Tell me if this is stupid. I'm sure there's a simpler and faster way
    > to do this, but I'm interested in knowing whether the idea itself
    > is... well, stupid.


    [...]

    > Basically, when I call obj.quacks_like? Klass I ask if every public
    > method defined in Klass is defined in obj as well.


    I don't think it's what you want. I won't call it stupid, but note that
    StringIO can be used as an IO for most things -- but not all things. In
    general, the duck typing philosophy seems to be "does it quack this way
    at this time" -- being concerned only about a single method at a time.

    -austin
    --
    Austin Ziegler *
    * Alternate:
    Austin Ziegler, Oct 18, 2005
    #2
    1. Advertising

  3. Daniel Schierbeck wrote:
    > class Object
    > def quacks_like? (klass)
    > klass.public_instance_methods.each do |method|
    > return false unless respond_to? method
    > end
    > return true
    > end
    > end


    Austin's post answers your question, but I'd just like to point out
    that this method could more cleanly be implemented as:

    class Object
    def quacks_like? (klass)
    klass.public_instance_methods.all? { |method| respond_to? method
    }
    end
    end
    Kevin Ballard, Oct 18, 2005
    #3
  4. On Oct 18, 2005, at 10:51 AM, Daniel Schierbeck wrote:

    > Tell me if this is stupid. I'm sure there's a simpler and faster
    > way to do this, but I'm interested in knowing whether the idea
    > itself is... well, stupid.
    >


    [snip]

    > Basically, when I call obj.quacks_like? Klass I ask if every public
    > method defined in Klass is defined in obj as well.


    No, not stupid. Why do you want to know that?

    >
    >
    > Cheers,
    > Daniel
    >
    >


    ----
    Bob Hutchison -- blogs at <http://www.recursive.ca/hutch/>
    Recursive Design Inc. -- <http://www.recursive.ca/>
    Raconteur -- <http://www.raconteur.info/>
    Bob Hutchison, Oct 18, 2005
    #4
  5. Bob Hutchison wrote:
    >
    > On Oct 18, 2005, at 10:51 AM, Daniel Schierbeck wrote:
    >
    >> Tell me if this is stupid. I'm sure there's a simpler and faster way
    >> to do this, but I'm interested in knowing whether the idea itself
    >> is... well, stupid.
    >>

    >
    > [snip]
    >
    >> Basically, when I call obj.quacks_like? Klass I ask if every public
    >> method defined in Klass is defined in obj as well.

    >
    >
    > No, not stupid. Why do you want to know that?


    Let's just say I have a history of proposing silly things :)
    Daniel Schierbeck, Oct 18, 2005
    #5
  6. Daniel Schierbeck

    Eric Mahurin Guest

    --- Austin Ziegler <> wrote:

    > > Basically, when I call obj.quacks_like? Klass I ask if

    > every public
    > > method defined in Klass is defined in obj as well.

    >=20
    > I don't think it's what you want. I won't call it stupid, but
    > note that
    > StringIO can be used as an IO for most things -- but not all
    > things. In
    > general, the duck typing philosophy seems to be "does it
    > quack this way
    > at this time" -- being concerned only about a single method
    > at a time.


    To elaborate a little more, I think the normal time to think
    about duck typing is when looking at the arguments of a method.
    If that method only needs certain methods from that argument,
    any object that can does these (in the way used) could be used
    as a "duck".

    In practice you'll find there is almost never a need to make
    sure an object has an entire interface of methods. And even
    when you want the object to respond to certain methods, the
    duck-typing philosophy is to not worry about it with any code -
    just let the check occur automatically when you try invoking
    methods on the object/duck.

    I usually write my methods with duck-typing in mind. For each
    argument, I try to apply a minimal number of methods to it.=20
    And pick method names that allow a variety of objects (i.e.
    #[]). For example, if you expect something like a string for
    an argument and only use #[], you'll find that that argument
    can also be an Array, Hash, ... and the best: a Proc. That's
    the power of duck-typing.






    =09
    =09
    __________________________________=20
    Yahoo! Mail - PC Magazine Editors' Choice 2005=20
    http://mail.yahoo.com
    Eric Mahurin, Oct 18, 2005
    #6
    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. Duck Typing

    , Sep 16, 2003, in forum: Python
    Replies:
    10
    Views:
    680
    Steve Holden
    Sep 17, 2003
  2. Replies:
    15
    Views:
    560
    Isaac Gouy
    Sep 1, 2006
  3. Replies:
    2
    Views:
    318
    Terry Reedy
    Jan 28, 2008
  4. Paddy
    Replies:
    1
    Views:
    389
    Paddy
    May 9, 2008
  5. Paddy
    Replies:
    0
    Views:
    240
    Paddy
    Jun 25, 2008
Loading...

Share This Page