# Problem with case statements

**URL:** https://rubytalk.org/t/problem-with-case-statements/43199
**Category:** ruby-talk
**Created:** [21 December 2007 11:14 UTC](https://rubytalk.org/t/problem-with-case-statements/43199 "2007-12-21T11:14:58Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)
#### Post date: [21 December 2007 11:14 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/1 "2007-12-21T11:14:58Z")

</div>

I have an innocent looking code:

54: def find(\*args)  
55: options = args.extract\_options!  
56: validate\_find\_options(options)  
57: case args.first  
58: when :first: find\_first(options)  
59: when :all: find\_all(options)  
60: else raise "Invalid find"  
61: end  
62: end

Now, above case statement totally blows up with Ruby 1.9:

/home/hemant/push\_server/lib/db\_connection.rb:61: warning: else without  
rescue is useless  
/home/hemant/push\_server/bin/boot.rb:34:in  
`require': /home/hemant/push\_server/lib/db\_connection.rb:58: syntax  
error, unexpected ':', expecting keyword\_then or ',' or ';' or  
'\n' (SyntaxError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :first: find\_first(options)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
/home/hemant/push\_server/lib/db\_connection.rb:59: syntax error,  
unexpected keyword\_when, expecting keyword\_end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :all: find\_all(options)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
/home/hemant/push\_server/lib/db\_connection.rb:87: syntax error,  
unexpected keyword\_end, expecting $end  
from /home/hemant/push\_server/bin/boot.rb:34:in `\<top (required)\>'

However if i rewrite it like this, it works:

&nbsp;&nbsp;&nbsp;&nbsp;def find(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options = args.extract\_options!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_find\_options(options)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case args.first  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :first  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_first(options)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :all  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_all(options)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Invalid find"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

Why is this? Was this intended?

---

<div class="post-metadata">

### Author: ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)
#### Post date: [21 December 2007 11:30 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/2 "2007-12-21T11:30:38Z")

</div>

> I have an innocent looking code:
> 
> 54: def find(\*args)  
> 55: options = args.extract\_options!  
> 56: validate\_find\_options(options)  
> 57: case args.first  
> 58: when :first: find\_first(options)  
> 59: when :all: find\_all(options)  
> 60: else raise "Invalid find"  
> 61: end  
> 62: end
> 
> Now, above case statement totally blows up with Ruby 1.9:

My test is with Ruby 1.8.6 but:

> /home/hemant/push\_server/bin/boot.rb:34:in  
> `require': /home/hemant/push\_server/lib/db\_connection.rb:58: syntax  
> error, unexpected ':', expecting keyword\_then or ',' or ';' or  
> '\n' (SyntaxError)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :first: find\_first(options)

if you want to put the body of the when in the same line you need a  
then keyword:

irb(main):018:0\> def find(\*args)  
irb(main):019:1\> case args.first  
irb(main):020:2\> when :first then puts "first"  
irb(main):021:2\> when :all then puts "all"  
irb(main):022:2\> else raise "invalid"  
irb(main):023:2\> end  
irb(main):024:1\> end  
=\> nil  
irb(main):025:0\> find :first, 1, 2, 3, 4  
first  
=\> nil

> However if i rewrite it like this, it works:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;def find(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options = args.extract\_options!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_find\_options(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case args.first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_first(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :all  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_all(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Invalid find"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> Why is this? Was this intended?

From the pickaxe:

"The then keyword (or a colon) separates the when comparisons from the  
bodies and is  
not needed if the body starts on a new line."

Hope this helps,

Jesus.

> **···**
>
> On Dec 21, 2007 12:14 PM, hemant kumar \<gethemant@gmail.com\> wrote:

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [21 December 2007 12:01 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/3 "2007-12-21T12:01:46Z")

</div>

> I have an innocent looking code:
> 
> 54: def find(\*args)  
> 55: options = args.extract\_options!  
> 56: validate\_find\_options(options)  
> 57: case args.first  
> 58: when :first: find\_first(options)  
> 59: when :all: find\_all(options)  
> 60: else raise "Invalid find"  
> 61: end  
> 62: end
> 
> Now, above case statement totally blows up with Ruby 1.9:
> 
> /home/hemant/push\_server/lib/db\_connection.rb:61: warning: else without  
> rescue is useless  
> /home/hemant/push\_server/bin/boot.rb:34:in  
> `require': /home/hemant/push\_server/lib/db\_connection.rb:58: syntax  
> error, unexpected ':', expecting keyword\_then or ',' or ';' or  
> '\n' (SyntaxError)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :first: find\_first(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
> /home/hemant/push\_server/lib/db\_connection.rb:59: syntax error,  
> unexpected keyword\_when, expecting keyword\_end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :all: find\_all(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
> /home/hemant/push\_server/lib/db\_connection.rb:87: syntax error,  
> unexpected keyword\_end, expecting $end  
> from /home/hemant/push\_server/bin/boot.rb:34:in `\<top (required)\>'

Does the same error surface if you precede the colon with a space?  
Sorry, I don't have a 1.9 here to test this myself.

> However if i rewrite it like this, it works:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;def find(\*args)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options = args.extract\_options!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_find\_options(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case args.first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_first(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when :all  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find\_all(options)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Invalid find"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> Why is this? Was this intended?

Maybe the form with colon is deprecated?

Kind regards

robert

> **···**
>
> 2007/12/21, hemant kumar \<gethemant@gmail.com\>:
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

### Author: ![Lee\_Jarvis2](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@Lee\_Jarvis2](https://rubytalk.org/u/Lee_Jarvis2)
#### Post date: [21 December 2007 11:48 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/4 "2007-12-21T11:48:33Z")

</div>

Jesús Gabriel y Galán wrote:

> if you want to put the body of the when in the same line you need a  
> then keyword:

1.8.6 works with a keyword or a colon, I think this is what Hemant  
means.. 1.9 obviously doesn't support using them (I can't test it right  
now)

> > def find(\*args)  
> > case args.first  
> > when :first: puts "first"  
> > when :all: puts "all"  
> > else raise "invalid"  
> > end  
> > end

=\> nil

> > find :first, 1, 2, 3, 4

first

As you can see, Hemants example works fine with colons in 1.8.6, perhaps  
not in 1.9, though...

I normally use 'then' in either case..

Regards,  
Lee

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Lee\_Jarvis2](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@Lee\_Jarvis2](https://rubytalk.org/u/Lee_Jarvis2)
#### Post date: [21 December 2007 11:49 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/5 "2007-12-21T11:49:02Z")

</div>

Jesús Gabriel y Galán wrote:

> if you want to put the body of the when in the same line you need a  
> then keyword:

1.8.6 works with a keyword or a colon, I think this is what Hemant  
means.. 1.9 obviously doesn't support using them (I can't test it right  
now)

> > def find(\*args)  
> > case args.first  
> > when :first: puts "first"  
> > when :all: puts "all"  
> > else raise "invalid"  
> > end  
> > end

=\> nil

> > find :first, 1, 2, 3, 4

first

As you can see, Hemants example works fine with colons in 1.8.6, perhaps  
not in 1.9, though...

I normally use 'then' in either case..

Regards,  
Lee

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)
#### Post date: [21 December 2007 13:17 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/6 "2007-12-21T13:17:26Z")

</div>

Looks like they deprecated ":" in case statements.

Matz, shall we consider this as bug or change was unintentional?

> **···**
>
> On Fri, 2007-12-21 at 21:01 +0900, Robert Klemme wrote:
> 
> > 2007/12/21, hemant kumar \<gethemant@gmail.com\>:  
> > \> I have an innocent looking code:  
> > \>  
> > \> 54: def find(\*args)  
> > \> 55: options = args.extract\_options!  
> > \> 56: validate\_find\_options(options)  
> > \> 57: case args.first  
> > \> 58: when :first: find\_first(options)  
> > \> 59: when :all: find\_all(options)  
> > \> 60: else raise "Invalid find"  
> > \> 61: end  
> > \> 62: end  
> > \>  
> > \>  
> > \> Now, above case statement totally blows up with Ruby 1.9:  
> > \>  
> > \> /home/hemant/push\_server/lib/db\_connection.rb:61: warning: else without  
> > \> rescue is useless  
> > \> /home/hemant/push\_server/bin/boot.rb:34:in  
> > \> `require': /home/hemant/push\_server/lib/db\_connection.rb:58: syntax  
> > \> error, unexpected ':', expecting keyword\_then or ',' or ';' or  
> > \> '\n' (SyntaxError)  
> > \> when :first: find\_first(options)  
> > \> ^  
> > \> /home/hemant/push\_server/lib/db\_connection.rb:59: syntax error,  
> > \> unexpected keyword\_when, expecting keyword\_end  
> > \> when :all: find\_all(options)  
> > \> ^  
> > \> /home/hemant/push\_server/lib/db\_connection.rb:87: syntax error,  
> > \> unexpected keyword\_end, expecting $end  
> > \> from /home/hemant/push\_server/bin/boot.rb:34:in `\<top (required)\>'
> > 
> > Does the same error surface if you precede the colon with a space?  
> > Sorry, I don't have a 1.9 here to test this myself.
> > 
> > \> However if i rewrite it like this, it works:  
> > \>  
> > \> def find(\*args)  
> > \> options = args.extract\_options!  
> > \> validate\_find\_options(options)  
> > \> case args.first  
> > \> when :first  
> > \> find\_first(options)  
> > \> when :all  
> > \> find\_all(options)  
> > \> else  
> > \> raise "Invalid find"  
> > \> end  
> > \> end  
> > \>  
> > \> Why is this? Was this intended?
> > 
> > Maybe the form with colon is deprecated?

---

<div class="post-metadata">

### Author: ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)
#### Post date: [21 December 2007 11:55 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/7 "2007-12-21T11:55:16Z")

</div>

Oh, you are right, I missed the colon after the symbol. ☹  
Sorry for the noise.

Jesus.

> **···**
>
> On Dec 21, 2007 12:48 PM, Lee Jarvis \<ljjarvis@gmail.com\> wrote:
> 
> > Jesús Gabriel y Galán wrote:  
> > \> if you want to put the body of the when in the same line you need a  
> > \> then keyword:
> > 
> > 1.8.6 works with a keyword or a colon, I think this is what Hemant  
> > means.. 1.9 obviously doesn't support using them (I can't test it right  
> > now)
> > 
> > \>\> def find(\*args)  
> > \>\> case args.first  
> > \>\> when :first: puts "first"  
> > \>\> when :all: puts "all"  
> > \>\> else raise "invalid"  
> > \>\> end  
> > \>\> end  
> > =\> nil  
> > \>\> find :first, 1, 2, 3, 4  
> > first
> > 
> > As you can see, Hemants example works fine with colons in 1.8.6, perhaps  
> > not in 1.9, though...  
> > I normally use 'then' in either case..

---

<div class="post-metadata">

### Author: ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)
#### Post date: [21 December 2007 13:18 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/8 "2007-12-21T13:18:50Z")

</div>

My bad.. i meant intentional, but you got the point anyways!.

Thanks

> **···**
>
> On Fri, 2007-12-21 at 18:47 +0530, hemant kumar wrote:
> 
> > On Fri, 2007-12-21 at 21:01 +0900, Robert Klemme wrote:  
> > \> 2007/12/21, hemant kumar \<gethemant@gmail.com\>:  
> > \> \> I have an innocent looking code:  
> > \> \>  
> > \> \> 54: def find(\*args)  
> > \> \> 55: options = args.extract\_options!  
> > \> \> 56: validate\_find\_options(options)  
> > \> \> 57: case args.first  
> > \> \> 58: when :first: find\_first(options)  
> > \> \> 59: when :all: find\_all(options)  
> > \> \> 60: else raise "Invalid find"  
> > \> \> 61: end  
> > \> \> 62: end  
> > \> \>  
> > \> \>  
> > \> \> Now, above case statement totally blows up with Ruby 1.9:  
> > \> \>  
> > \> \> /home/hemant/push\_server/lib/db\_connection.rb:61: warning: else without  
> > \> \> rescue is useless  
> > \> \> /home/hemant/push\_server/bin/boot.rb:34:in  
> > \> \> `require': /home/hemant/push\_server/lib/db\_connection.rb:58: syntax  
> > \> \> error, unexpected ':', expecting keyword\_then or ',' or ';' or  
> > \> \> '\n' (SyntaxError)  
> > \> \> when :first: find\_first(options)  
> > \> \> ^  
> > \> \> /home/hemant/push\_server/lib/db\_connection.rb:59: syntax error,  
> > \> \> unexpected keyword\_when, expecting keyword\_end  
> > \> \> when :all: find\_all(options)  
> > \> \> ^  
> > \> \> /home/hemant/push\_server/lib/db\_connection.rb:87: syntax error,  
> > \> \> unexpected keyword\_end, expecting $end  
> > \> \> from /home/hemant/push\_server/bin/boot.rb:34:in `\<top (required)\>'  
> > \>  
> > \> Does the same error surface if you precede the colon with a space?  
> > \> Sorry, I don't have a 1.9 here to test this myself.  
> > \>  
> > \> \> However if i rewrite it like this, it works:  
> > \> \>  
> > \> \> def find(\*args)  
> > \> \> options = args.extract\_options!  
> > \> \> validate\_find\_options(options)  
> > \> \> case args.first  
> > \> \> when :first  
> > \> \> find\_first(options)  
> > \> \> when :all  
> > \> \> find\_all(options)  
> > \> \> else  
> > \> \> raise "Invalid find"  
> > \> \> end  
> > \> \> end  
> > \> \>  
> > \> \> Why is this? Was this intended?  
> > \>  
> > \> Maybe the form with colon is deprecated?  
> > \>
> > 
> > Looks like they deprecated ":" in case statements.
> > 
> > Matz, shall we consider this as bug or change was unintentional?

---

<div class="post-metadata">

### Author: ![F\_Senault](https://avatars.discourse-cdn.com/v4/letter/f/c37758/32.png) [@F\_Senault](https://rubytalk.org/u/F_Senault)
#### Post date: [21 December 2007 13:55 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/9 "2007-12-21T13:55:04Z")

</div>

According to the 3rd edition of the PickAxe, it is intentional.

(p 125, Case Expressions : "Ruby 1.8 allowed you to use a colon  
character in place of the then keyword. This is no longer supported.")

Fred

> **···**
>
> Le 21 décembre à 14:18, hemant kumar a écrit :
> 
> > My bad.. i meant intentional, but you got the point anyways!.
> 
> --  
> Something inside of me has opened up its eyes  
> Why did you put it there did you not realize  
> This thing inside of me it screams the loudest sound  
> Sometimes I think I could (Nine Inch Nails, Burn)

---

<div class="post-metadata">

### Author: ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)
#### Post date: [21 December 2007 14:06 UTC](https://rubytalk.org/t/problem-with-case-statements/43199/10 "2007-12-21T14:06:47Z")

</div>

Ha ha.. damn to its decided. I don't own third edition apparently.

> **···**
>
> On Fri, 2007-12-21 at 22:55 +0900, F. Senault wrote:
> 
> > Le 21 décembre à 14:18, hemant kumar a écrit :
> > 
> > \> My bad.. i meant intentional, but you got the point anyways!.
> > 
> > According to the 3rd edition of the PickAxe, it is intentional.
> > 
> > (p 125, Case Expressions : "Ruby 1.8 allowed you to use a colon  
> > character in place of the then keyword. This is no longer supported.")
