From: ara.t.howard@... Date: 2006-07-28T23:52:58+09:00 Subject: Re: Weird problem with case expressions 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} regards. -a -- we can never obtain peace in the outer world until we make peace with ourselves. - h.h. the 14th dali lama