case statement puzzle - correction

T

Tom Cloyd

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
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
F

F. Senault

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 :
1
=> nil

Fred
 
T

Tom Cloyd

Matthew said:
Seconded. You could also have said:

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


In ruby, parenthesis are optional.... except when they aren't. ;-)

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
<< (e-mail address removed) >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

case statement puzzle 8
Whaaaaat? 19
minor problem with 1.9 compile 3
rdoc run-time length question 11
class 'initialize' method not working ??? 8
gsub and regex 2
how to stop gsub from returning nil 9
regex \s == \n??? 11

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top