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 C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

Le 16 juillet 2009 à 18:58, Tom C. 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

Fred

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

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

Matthew K. Williams wrote:

end
=> 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 C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs