From: Rob Biedenharn Date: 2009-07-17T04:30:41+09:00 Subject: Re: case statement puzzle On Jul 16, 2009, at 12:49 PM, Tom Cloyd wrote: > I'm missing something here, and cannot see the problem: > > x='1' > (1..5).include? x.to_i # => true > > But... > > x='1' > case > when x =='0' > puts '0' > when (1..5).include? x.to_i > puts '1' > end > > ...won't even compile. Can someone tell me why? (and maybe how to > fix it...) > > What I'm having to do is this, which works: > > ... > when (1,,5).to_a & [x.to_i].length > 0 > ... > > but it seems over-wrought. > > t. > -- > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist > Bellingham, Washington, U.S.A: (360) 920-1226 > << tc@tomcloyd.com >> (email) > << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental > health weblog) > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ x='1' case when x =='0' puts '0' when (1..5).include?(x.to_i) puts '1' end If you don't leave the parentheses off of the .include? method it will work. The better answers boil down to "learn about the case-equality method ===", but using parentheses will at least get around the ambiguous parsing. You should probably also consider that the value of the entire case statement, as written, will be nil since that is the "return value" of the puts method as well as the value if none of the 'when' clauses match. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com P.S. For completeness: x = '1' case x when '0' puts '0' when '1'..'5' puts '1' end