case statement puzzle

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

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

James Coglan

[Note: parts of this message were removed to make it a legal post.]

2009/7/16 Tom Cloyd said:
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


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.
 
T

Tom Cloyd

James said:
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.
THANKS! That's the "missing piece" - I need to study up on "===", about
which I know nothing.

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

Rick DeNatale

What about:

x=3D'1'
case
when x =3D=3D'0'
=A0puts '0'
when '1'..'5'
=A0puts '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




--=20
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
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]

2009/7/16 Rick DeNatale said:
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


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

Rob Biedenharn

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
<< (e-mail address removed) >> (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
(e-mail address removed)


P.S. For completeness:

x = '1'
case x
when '0'
puts '0'
when '1'..'5'
puts '1'
end
 
B

Bertram Scharpf

Hi,

Am Freitag, 17. Jul 2009, 02:12:27 +0900 schrieb Tom Cloyd:
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
 
T

Tom Cloyd

Bertram said:
Hi,

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


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
Thanks...you're right, of course, and I soon learned that, in this
morning ruby exercise session. Very interesting!

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

Bertram Scharpf

Hi,

Am Freitag, 17. Jul 2009, 07:01:07 +0900 schrieb Tom Cloyd:
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
 

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


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top