Duck-typing

D

Daniel Schierbeck

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
 
A

Austin Ziegler

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
 
K

Kevin Ballard

Daniel said:
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
 
B

Bob Hutchison

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

Daniel Schierbeck

Bob said:
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 :)
 
E

Eric Mahurin

--- Austin Ziegler said:
=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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top