From: ara.t.howard@... Date: 2006-07-29T00:53:22+09:00 Subject: Re: Weird problem with case expressions On Sat, 29 Jul 2006, Daniel Schierbeck wrote: >> althogh i personally would use >> >> [Integer, String, Array].detect{|c| c === 'foo'} >> >> since i can easily become a one liner, even with many classes. if you need >> the string then >> >> [Integer, String, Array].detect{|c| c === 'foo' and c.name.downcase} > > That would work, but I don't think classes and types are always the same. An > ordered hash is still a hash, even if it doesn't descend from Hash. This > way, all objects that respond to #to_hash is in some way a hash, and I can > trust (to some degree) that the object returned when calling that method > implements the interface of Hash. indeed. still, the technique is more compact than a case: %w( to_int to_str to_hash to_ary ).detect{|m| 'foo'.respond_to? m} and, for returning values { 'to_int' => 'integer', 'to_str' => 'string', 'to_hash' => 'hash', }.detect{|m,s| break s if 'foo'.respond_to? m} note that this returns 'nil' when no match is found. but this isn't quite as compact. cheers. -a -- we can never obtain peace in the outer world until we make peace with ourselves. - h.h. the 14th dali lama