Case statement puzzle - correction

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 # <-- ERROR CORRECTED

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

When in doubt, parenthesise :

case

?> when x == '0'

      puts '0'
    when (1..5).include?(x.to_i)
      puts '1'
    end

1
=> nil

Fred

···

Le 16 juillet 2009 à 18:58, Tom Cloyd a écrit :
--
Until I'm 6 feet under Baby I don't need a bed Gonna live while I'm
alive I'll sleep when I'm dead Till they roll me over And lay my
bones to rest Gonna live while I'm alive I'll sleep when I'm dead
                                    (I'll sleep when I'm dead, Bon Jovi)

Seconded. You could also have said:

when ((1..5).include? x.to_i)

In ruby, parenthesis are optional.... except when they aren't. :wink:

Matt

···

On Fri, 17 Jul 2009, F. Senault wrote:

Le 16 juillet 2009 à 18:58, Tom Cloyd a écrit :

x='1'
case
when x =='0'
  puts '0'
when (1..5).include? x.to_i
  puts '1'
end

When in doubt, parenthesise :

case

?> when x == '0'

      puts '0'
    when (1..5).include?(x.to_i)
      puts '1'
    end

1
=> nil

Matthew K. Williams wrote:

···

On Fri, 17 Jul 2009, F. Senault wrote:

Le 16 juillet 2009 à 18:58, Tom Cloyd a écrit :

x='1'
case
when x =='0'
  puts '0'
when (1..5).include? x.to_i
  puts '1'
end

When in doubt, parenthesise :

case

?> when x == '0'

      puts '0'
    when (1..5).include?(x.to_i)
      puts '1'
    end

1
=> nil

Seconded. You could also have said:

when ((1..5).include? x.to_i)

In ruby, parenthesis are optional.... except when they aren't. :wink:

Matt

Fred, Matt,

Yeah. It was amazing how many problems I just solved with a few parentheses. Yikes! No syntactic sugar here...but the code's working, so all's well.

Thanks so much for the quick, accurate diagnosis.

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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~