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

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.

···

2009/7/16 Tom Cloyd <tomcloyd@comcast.net>

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

--
James Coglan
http://jcoglan.com

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
Rob@AgileConsultingLLC.com

P.S. For completeness:

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

···

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

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
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

James Coglan wrote:

···

2009/7/16 Tom Cloyd <tomcloyd@comcast.net>

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.

--
James Coglan
http://jcoglan.com

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
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

···

On Thu, Jul 16, 2009 at 12:55 PM, James Coglan<jcoglan@googlemail.com> wrote:

What about:

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

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

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

···

2009/7/16 Rick DeNatale <rick.denatale@gmail.com>

On Thu, Jul 16, 2009 at 12:55 PM, James Coglan<jcoglan@googlemail.com> > 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

Hi,

James Coglan wrote:

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

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

···

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

2009/7/16 Tom Cloyd <tomcloyd@comcast.net>

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Bertram Scharpf wrote:

Hi,

James Coglan wrote:
    

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

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

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

t.

···

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

2009/7/16 Tom Cloyd <tomcloyd@comcast.net>

--

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

Hi,

Bertram Scharpf wrote:

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
  

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

···

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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de