From: Daniel Schierbeck Date: 2006-07-29T00:30:06+09:00 Subject: Re: Weird problem with case expressions ara.t.howard@noaa.gov wrote: > On Fri, 28 Jul 2006, Daniel Schierbeck wrote: > >> Thank you for the response. Yes, it seems there is no way to get >> around using parentheses. It's actually just an aesthetic problem; the >> method in question has a question mark suffix, and I like to call such >> methods without parentheses. Specifically, it was an attempt at an >> aesthetically pleasing version of the code in "Symbols are your friend": >> >> case "foo" >> when of_type? :int then "integer" >> when of_type? :str then "string" >> end >> >> Unfortunately, I'll have to write it like this: >> >> case "foo" >> when of_type?(:int) then "integer" >> when of_type?(:str) then "string" >> end >> >> It doesn't really matter that much though -- I'm not even sure I like >> that solution. >> >> >> Cheers, and thanks for replying, >> Daniel > > you could do > > class Symbol > def like? > { > :int => Integer, > :str => String, > :ary => Array, > }[self] or raise 'no type!' > end > end > > then > > case 'foo' > when :int.like? then 'integer' > when :str.like? then 'string' > end > > > 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. Cheers, Daniel