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.
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
···
On 10/18/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca
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?
···
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
···
On Oct 18, 2005, at 10:51 AM, Daniel Schierbeck wrote:
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.
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.
···
--- Austin Ziegler <halostatue@gmail.com> wrote:
> 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.
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005