# Case statement puzzle - correction

**URL:** <https://www.ruby-forum.com/t/case-statement-puzzle-correction/172205>\
**Category:** Ruby\
**Created:** [July 16, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle-correction/172205 "2009-07-16T18:59:43Z")\
**Posts on this page:** 4\
**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:59pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle-correction/172205/1 "2009-07-16T18:59:43Z")

</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 # \<-- ERROR CORRECTED  
…

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, 7:20pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle-correction/172205/2 "2009-07-16T19:20:45Z")

</div>

Le 16 juillet 2009 à 18:58, Tom C. a écrit :

> x=‘1’  
> case  
> when x ==‘0’  
> puts ‘0’  
> when (1…5).include? x.to\_i  
> puts ‘1’  
> end

When in doubt, parenthesise :

> > case  
> > ?\> when x == ‘0’
> 
> > ```
> > puts '0'
> > when (1..5).include?(x.to_i)
> > puts '1'
> > end
> > 
> > ```

1  
=\> nil

Fred

---

<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:31pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle-correction/172205/3 "2009-07-16T19:31:51Z")

</div>

On Fri, 17 Jul 2009, F. Senault wrote:

> When in doubt, parenthesise :
> 
> > > case  
> > > ?\> when x == ‘0’
> > 
> > > ```
> > > puts '0'
> > > when (1..5).include?(x.to_i)
> > > puts '1'
> > > end
> > > 
> > > ```
> 
> 1  
> =\> nil

Seconded. You could also have said:

when ((1…5).include? x.to\_i)

In ruby, parenthesis are optional… except when they aren’t. 😉

Matt

---

<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, 8:29pm UTC](https://www.ruby-forum.com/t/case-statement-puzzle-correction/172205/4 "2009-07-16T20:29:42Z")

</div>

Matthew K. Williams wrote:

> > > end  
> > > =\> nil
> 
> 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.

–

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

```
