Easy Duck-Typing


#1

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


#2

Hi –

On Fri, 16 Dec 2005, Daniel S. wrote:

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


David A. Black
removed_email_address@domain.invalid

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!


#3

On 12/15/05, Daniel S. removed_email_address@domain.invalid wrote:

   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.


#4

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

#5

Hi –

On Fri, 16 Dec 2005, Mark E. wrote:

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.

David


David A. Black
removed_email_address@domain.invalid

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!


#6

On Fri, 16 Dec 2005 removed_email_address@domain.invalid wrote:

Duck typing is really about the programmer, not the object.

what is the sound of one duck flapping?

-a


#7

Hi –

On Fri, 16 Dec 2005, removed_email_address@domain.invalid wrote:

On Fri, 16 Dec 2005 removed_email_address@domain.invalid wrote:

Duck typing is really about the programmer, not the object.

what is the sound of one duck flapping?

Meow.

David


David A. Black
removed_email_address@domain.invalid

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!


#8

On 12/15/05, removed_email_address@domain.invalid removed_email_address@domain.invalid wrote:

On Fri, 16 Dec 2005 removed_email_address@domain.invalid wrote:

Duck typing is really about the programmer, not the object.

what is the sound of one duck flapping?

Chunky Bacon of course!

-a


#9

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2005.12.16 02:44, Mark E. removed_email_address@domain.invalid wrote:

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 :slight_smile:

  • Mark

E
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDogyQxvA1l6h+MUMRAuCTAJ9giSseOXvQ9Jvm9gAUw27XJz8pjwCZAede
bxQ5bNkjcG9gi9ra8ZKp4mM=
=mtOF
-----END PGP SIGNATURE-----


#10

removed_email_address@domain.invalid wrote:

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 :slight_smile:

Cheers,
Daniel