# Case statement puzzle

**URL:** <https://www.ruby-forum.com/t/case-statement-puzzle/172204>\
**Category:** Ruby\
**Created:** [July 16, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204 "2009-07-16T18:49:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 16, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/1 "2009-07-16T18:49:47Z")

</div>

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.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 16, 2009, 6:56pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/2 "2009-07-16T18:56:48Z")

</div>

2009/7/16 Tom C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 16, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/3 "2009-07-16T19:14:20Z")

</div>

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](http://jcoglan.com)

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

t.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 16, 2009, 9:00pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/4 "2009-07-16T21:00:41Z")

</div>

On Thu, Jul 16, 2009 at 12:55 PM, James C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 16, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/5 "2009-07-16T21:31:39Z")

</div>

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](http://TomCloyd.com) \>\> (website) \<\< [sleightmind.wordpress.com](http://sleightmind.wordpress.com) \>\> (mental  
> health weblog)
> 
> ```auto
> 
> ```

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

P.S. For completeness:

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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 16, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/6 "2009-07-16T21:27:34Z")

</div>

2009/7/16 Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 16, 2009, 11:00pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/7 "2009-07-16T23:00:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 17, 2009, 12:01am UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/8 "2009-07-17T00:01:51Z")

</div>

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.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [July 17, 2009, 5:15am UTC](https://www.ruby-forum.com/t/case-statement-puzzle/172204/9 "2009-07-17T05:15:34Z")

</div>

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
