Case statement puzzle

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

2009/7/16 Tom C. [email protected]

puts ‘0’
when (1…5).include? x.to_i
puts ‘1’
end

What about:

x=‘1’
case
when x ==‘0’
puts ‘0’
when ‘1’…‘5’
puts ‘1’
end

The basic idea is that anything following ‘when’ should respond to ‘===’
to
indicate membership, or type/pattern-matching. e.g. Module#=== tells you
whether the argument is an instance of the receiving module, Regexp#===
tests a regex against a string.

James C. wrote:

x=‘1’

whether the argument is an instance of the receiving module, Regexp#===
tests a regex against a string.


James C.
http://jcoglan.com

THANKS! That’s the “missing piece” - I need to study up on “===”, about
which I know nothing.

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)

On Thu, Jul 16, 2009 at 12:55 PM, James C.[email protected]
wrote:

What about:

x=‘1’
case
when x ==‘0’
puts ‘0’
when ‘1’…‘5’
puts ‘1’
end

Almost, that second when will ALWAYS match.

Better to use the form of case which takes a value

%w{0 1 2}.each do |x|
case x
when ‘0’
puts “it’s like nothing, man!”
when ‘1’…‘5’
puts “it’s in there”
end
end

it’s like nothing, man!
it’s in there
it’s in there


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On Jul 16, 2009, at 12:49 PM, Tom C. wrote:

puts ‘0’
when (1,5).to_a & [x.to_i].length > 0
<< 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 B. http://agileconsultingllc.com
[email protected]

P.S. For completeness:

x = ‘1’
case x
when ‘0’
puts ‘0’
when ‘1’…‘5’
puts ‘1’
end

2009/7/16 Rick DeNatale [email protected]

end
puts “it’s in there”
end
end

it’s like nothing, man!
it’s in there
it’s in there

Whoops, I wasn’t reading closely enough, just focused on the failing
when
clause. Thanks for the correction.

Hi,

Am Freitag, 17. Jul 2009, 02:12:27 +0900 schrieb Tom C.:

THANKS! That’s the “missing piece” - I need to study up on “===”, about
which I know nothing.

This will output “1” because the Range object ‘1’…‘5’ is not nil
and not false. It has nothing to do with the === operator. You
could also write

x=‘1’
case
when x == ‘0’
puts ‘0’
when “hi, there”
puts ‘1’
end

To apply the === operator you have to mention the variable
after the “case” keyword.

x = ‘1’
case x
when ‘0’ then puts ‘0’
when ‘1’…‘5’ then puts ‘1’
end

Bertram

Bertram S. wrote:

when x ==‘0’
puts ‘0’
and not false. It has nothing to do with the === operator. You
To apply the === operator you have to mention the variable

Thanks…you’re right, of course, and I soon learned that, in this
morning ruby exercise session. Very interesting!

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)

Hi,

Am Freitag, 17. Jul 2009, 07:01:07 +0900 schrieb Tom C.:

Thanks…you’re right, of course, and I soon learned that, in this morning
ruby exercise session. Very interesting!

Yes, learning is the only straight fun.

Returning to your initial problem: Maybe it could be a reasonable
solution to say

if x.to_i.nonzero? then
do_it_with x
end

As I mention Fixnum#nonzero?: Furthermore, I’m convinced there
should be a corresponding String#notempty? method. And
Array#notempty?.

Bertram