Easy Duck-Typing

D

Daniel Schierbeck

This will make it easier to add type checking to your methods. Let me
know if you think the method belongs in Object or another class, rather
than in the Kernel module.


class Kernel
def cast(obj, klass, method = nil)
return obj if obj.kind_of? klass

method = "to_#{method || klass.downcase}"

unless obj.respond_to? method
raise TypeError, "Can't convert #{obj.class} into #{klass}"
end

cast_obj = obj.send(method)

unless ccast_obj.kind_of? klass
raise TypeError, "#{obj.class}##{method} should return #{klass}"
end

return cast_obj
end
end


class Klass
def foo(arg)
# Will raise an exception unless `arg' is
# of type SomeKlass, or if it has a method
# #to_some_klass that returns an object of
# type SomeKlass
arg = cast(arg, SomeKlass, :some_klass)
end
end


Is there an easy way to turn a CapitalizedName into a downcase_name?
Right now I'm just downcasing the class name if no cast method name is
explicitly provided, but I'm not sure that's enough.


Cheers,
Daniel
 
D

dblack

Hi --

This will make it easier to add type checking to your methods. Let me know if
you think the method belongs in Object or another class, rather than in the
Kernel module.

This may be useful to you, but it isn't duck typing. In fact, it
appears to be more or less the opposite, in concept. Here, you're
checking class ancestry and gatekeeping your method calls and return
values based on that ancestry. The thrust of duck typing, as I
understand it, is that you just ask an object to do something --
without regard to its class, and quite likely without doing a
respond_to? call first.


David
 
E

Eric Mahurin

class Kernel
def cast(obj, klass, method =3D nil)
return obj if obj.kind_of? klass

method =3D "to_#{method || klass.downcase}"

unless obj.respond_to? method
raise TypeError, "Can't convert #{obj.class} into #{klass}"
end

cast_obj =3D obj.send(method)

unless ccast_obj.kind_of? klass
raise TypeError, "#{obj.class}##{method} should return #{klass}"
end

return cast_obj
end
end

Like David said this is basically the opposite of duck-typing. The
idea of duck-typing is that the code should completely ignore the type
(however you want to define it). You just let ruby raise an exception
if the object doesn't respond to the methods you need from the
objects.

I've heard the misconception before that duck-typing is most useful
for type casting (i.e. #to_s method). To me, that is just a
conveinence (the caller could just as easy have explicitly done the
conversion). That is not where the power and beauty of duck-typing
lies. It really is a quite flexible polymorphic concept.
 
M

Mark Ericson

The thrust of duck typing, as I
understand it, is that you just ask an object to do something --
without regard to its class, and quite likely without doing a
respond_to? call first.

I've pondered if duck typing is about an object being able to respond
to a specific method or if it is about a set of methods. I also
wonder if a mechanism like Objective-C's 'protocol' would be useful
in Ruby.

- Mark
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top