Forum: Ruby Easy Duck-Typing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-15 17:00
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2005-12-15 17:21
(Received via mailing list)
Hi --

On Fri, 16 Dec 2005, Daniel Schierbeck 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
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
C35ede9febe86d05f8b45c67191de495?d=identicon&s=25 Eric Mahurin (Guest)
on 2005-12-15 18:34
(Received via mailing list)
On 12/15/05, Daniel Schierbeck <daniel.schierbeck@gmail.com> 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.
Aa1f23332dbb5408a55ed190648ba172?d=identicon&s=25 Mark Ericson (Guest)
on 2005-12-15 18:46
(Received via mailing list)
> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2005-12-15 19:25
(Received via mailing list)
Hi --

On Fri, 16 Dec 2005, Mark Ericson 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
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2005-12-15 19:31
(Received via mailing list)
On Fri, 16 Dec 2005 dblack@wobblini.net wrote:

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

what is the sound of one duck flapping?

-a
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2005-12-15 20:25
(Received via mailing list)
Hi --

On Fri, 16 Dec 2005, ara.t.howard@noaa.gov wrote:

> On Fri, 16 Dec 2005 dblack@wobblini.net wrote:
>
>> Duck typing is really about the programmer, not the object.
>
> what is the sound of one duck flapping?

Meow.


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-15 20:34
(Received via mailing list)
dblack@wobblini.net 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 :)


Cheers,
Daniel
A777f1a2049d78a12ead38efb8f75f97?d=identicon&s=25 Tanner Burson (Guest)
on 2005-12-15 20:37
(Received via mailing list)
On 12/15/05, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
>
> On Fri, 16 Dec 2005 dblack@wobblini.net wrote:
>
> > Duck typing is really about the programmer, not the object.
>
> what is the sound of one duck flapping?


Chunky Bacon of course!

-a
5c7bdd14d6885c8275eaf78be41d120a?d=identicon&s=25 Eero Saynatkari (Guest)
on 2005-12-16 04:27
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2005.12.16 02:44, Mark Ericson <mark.ericson@gmail.com> 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 :)

> - Mark


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

iD8DBQFDogyQxvA1l6h+MUMRAuCTAJ9giSseOXvQ9Jvm9gAUw27XJz8pjwCZAede
bxQ5bNkjcG9gi9ra8ZKp4mM=
=mtOF
-----END PGP SIGNATURE-----
This topic is locked and can not be replied to.