# Case when when

**URL:** <https://www.ruby-forum.com/t/case-when-when/119935>\
**Category:** Ruby\
**Created:** [November 12, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/case-when-when/119935 "2007-11-12T12:37:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 12, 2007, 12:37pm UTC](https://www.ruby-forum.com/t/case-when-when/119935/1 "2007-11-12T12:37:37Z")

</div>

Hi all

I am just wondering how can we make two cases belong to the same bunch  
of code. I tried to do it like c but it did not work. it is like this in  
c

switch(x)  
{  
case 5:  
#do bla bla  
break;  
case 7 : case 9:  
#do bla bla  
break;  
default:  
#bla  
}

however, I tried to do the same way in ruby but it did not work.  
case x  
when 5:  
#do bla  
when 7: when 9:  
#do bla bla  
else  
#bla  
end

I know that when provides conditions and I can use them but I am just  
wondering what is the way because i did not find it in books and online

regards

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 12, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/case-when-when/119935/2 "2007-11-12T12:45:52Z")

</div>

Hi Shuaib,

> I know that when provides conditions and I can use them but I am just  
> wondering what is the way because i did not find it in books and online

case x  
when 5,6,7: puts ‘a’  
when 10,11: puts ‘b’  
end

HTH,  
Peter

* * *

[http://www.rubyrailways.com](http://www.rubyrailways.com)  
[http://scrubyt.org](http://scrubyt.org)

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 12, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/case-when-when/119935/3 "2007-11-12T16:21:05Z")

</div>

2007/11/12, Shuaib Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> break;  
> #do bla  
> when 7: when 9:  
> #do bla bla  
> else  
> #bla  
> end
> 
> I know that when provides conditions and I can use them but I am just  
> wondering what is the way because i did not find it in books and online

case foo  
when 1, 2, 10  
then …  
else  
…  
end

Cheers

robert

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [November 13, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/case-when-when/119935/4 "2007-11-13T13:21:56Z")

</div>

case x  
when 5…9: puts ‘a’ # 5,6,7,8,9  
when 10…14: puts ‘b’ # 10,11,12,13  
else puts ‘c’  
end

# x = 5 then |\> a

# x = 10 then |\> b

# x = 14 then |\> c
