Hi --

On Fri, 2 Mar 2007, dblack / wobblini.net wrote:

> Hi --
>
> On Fri, 2 Mar 2007, Gary Wright wrote:
>
>> On Mar 1, 2007, at 7:38 PM, James Edward Gray II wrote:
>>> You're still type checking, you're just doing it in a more fragile 
>>> way. If you want to type check, use the class, I say.
>> 
>> Yet if I test for (Hash == mystery_obj) that would not
>> allow someone to pass an RBTree object instead, which I think
>> is a very reasonable thing to allow and works just fine if
>> I only use #fetch.
>
> I had the impression James was talking about the Integer and String
> methods, though then again those aren't actually the classes.  So I'm
> not sure what he meant :-)  But I don't think it was just to test
> class membership, since that manifestly doesn't help in the kind of
> situation you're describing.

Well, I should say: it's a way to deal with some of the practicalities
of a situation where you really only want objects of certain classes,
at the expense of duck typing.  But (a) it sounds like you want
something more elastic, and (b) testing class membership doesn't tell
you anything definitive, so it doesn't solve the problem if you're
thinking that rogue objects might be coming in to the method (since if
someone can roguely send it, say, a Proc, which responds to [], they
can presumably send it a hash that responds to [] irresponsibly).

I guess I tend to think in terms of error handling: that is, let
objects call [], but catch the ones that fail, or the ones that hand
back nonsense (in the context) values.

It's funny sometimes how discussions of duck typing come at the same
thing from two directions: protecting systems from supposed gremlins
that are engineering its demise by extending objects with destructive
but well-camouflaged behaviors, and exploring the coolness of the
openness of Ruby objects.  Or something.


David

-- 
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)