# Why cannot i put conditions in when clause

**URL:** <https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648>\
**Category:** Ruby\
**Created:** [June 5, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648 "2008-06-05T19:31:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [June 5, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648/1 "2008-06-05T19:31:22Z")

</div>

irb(main):003:0\> case a  
irb(main):004:1\> when 0  
irb(main):005:1\> puts “0”  
irb(main):006:1\> when \>0  
irb(main):007:1\> puts “greater”  
irb(main):008:1\> end  
SyntaxError: compile error  
(irb):6: syntax error, unexpected ‘\>’  
when \>0  
^  
from (irb):8

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [June 5, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648/2 "2008-06-05T19:40:46Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Junkone wrote:  
| irb(main):003:0\> case a  
| irb(main):004:1\> when 0  
| irb(main):005:1\> puts “0”  
| irb(main):006:1\> when \>0  
| irb(main):007:1\> puts “greater”  
| irb(main):008:1\> end  
| SyntaxError: compile error  
| (irb):6: syntax error, unexpected ‘\>’  
| when \>0  
| ^  
| from (irb):8  
|

That’s what if…else…end is there for.

In a case statement you usually check against ‘known’ and well-defined  
states. Also, once a condition is met in a case statement, no other  
branches are evaluated. So, if you had ‘when \< 1’ that wouldn’t be  
evaluated, even id a = 2.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

10 years old is a good age to get stuck at.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkhIJQMACgkQbtAgaoJTgL/PdACgl0ZgF5lF/X8wKPql1h/+Y+/s  
BbcAn3KLQdeiCg2iYEsx2jr1MzKsBLPY  
=L4oU  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [June 5, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648/3 "2008-06-05T19:41:57Z")

</div>

Junkone wrote:

> ```
> from (irb):8
> 
> ```

It’s not an expression.

You can do this:

case  
when a==0  
when a\>0  
end

or

case a  
when 0  
when 0…MAX\_EXPECTED\_VALUE\_FOR\_A  
end

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [June 5, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648/4 "2008-06-05T19:46:04Z")

</div>

On Jun 5, 2008, at 12:29 PM, Junkone wrote:

> irb(main):006:1\> when \>0

Idea #1:

when 0…(1.0/0.0)

Idea #2:

case # note that I have removed the a

# …

when a \> 0

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [June 5, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648/5 "2008-06-05T20:40:46Z")

</div>

On 05.06.2008 19:26, Junkone wrote:

> ```
> from (irb):8
> 
> ```

You can:

$ ruby -ce ‘case;when a == 0;puts “0”;when a \> 0;puts “greater”;end’  
Syntax OK

This is the second form of “case”.

Btw, there is also another way: define a criteria that implements === as

> 0:

irb(main):001:0\> POS = Object.new  
=\> #Object:0x7ff9e244  
irb(main):002:0\> def POS.===(x) x \> 0 end  
=\> nil  
irb(main):003:0\> a=10  
=\> 10  
irb(main):004:0\> case a  
irb(main):005:1\> when 0  
irb(main):006:1\> puts “0”  
irb(main):007:1\> when POS  
irb(main):008:1\> puts “positive”  
irb(main):009:1\> end  
positive  
=\> nil  
irb(main):010:0\>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [June 5, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648/6 "2008-06-05T19:55:35Z")

</div>

James G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Idea #1:
> 
> when 0…(1.0/0.0)

😉

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [June 5, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/why-cannot-i-put-conditions-in-when-clause/139648/7 "2008-06-05T21:16:16Z")

</div>

On Thu, Jun 5, 2008 at 1:40 PM, Joel VanderWerf  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can do this:
> 
> case  
> when a==0  
> when a\>0  
> end

First of all: huh. Learn something new about Ruby (almost) every day.

That said - why would you do this rather than an if/elsif? I’m  
genuinely curious. E.g.

if a == 0 then …  
elsif a\>0 then …  
end

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)
