From: James Edward Gray II Date: 2007-03-02T09:38:20+09:00 Subject: Re: Duck Typing Hash-Like Objects On Mar 1, 2007, at 6:31 PM, Gary Wright wrote: > class A > def initialize(arg, &b) > case > when arg.respond_to?(:nonzero?) > # do construction based on integer-like behavior Floats have nonzero?() too. I really think picking arbitrary methods like this to find a type is a big mistake. 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. If you want it to be an Integer, ask it if it can: Integer(...) rescue # nope... > when arg.respond_to?(:fetch) > # do construction based on hash-like behavior Arrays have fetch too. > when arg.respond_to?(:to_str) > # do construction based on string-like behavior String(...) rescue # nope... > else > # punt > end > end James Edward Gray II