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.
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.
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
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.
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.
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.
Here’s an example of duck typing:
def x(a)
puts a[1]
end
My x method takes an argument, and sends a message to that argument
without any ceremony or preliminaries of any kind.
Duck typing is really about the programmer, not the object.
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.
The smallest set of methods that are required for a
certain operation to complete as it is expected to
Mark
E
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
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
I guess you’re right, I actually realised it moments after i sent the
mail. I still find it useful though, when you really do need an object
of a specific type.
“Implicit conversion” may be better
Cheers,
Daniel
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.