# Case when when

**URL:** <https://rubytalk.org/t/case-when-when/42119>\
**Category:** ruby-talk\
**Created:** [12 November 2007 11:37 UTC](https://rubytalk.org/t/case-when-when/42119 "2007-11-12T11:37:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuaib\_Zahda](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@Shuaib\_Zahda](https://rubytalk.org/u/Shuaib_Zahda)\
**Post date:** [12 November 2007 11:37 UTC](https://rubytalk.org/t/case-when-when/42119/1 "2007-11-12T11:37:35Z")

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

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

---

<div class="post-metadata">

**Author:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [12 November 2007 11:44 UTC](https://rubytalk.org/t/case-when-when/42119/2 "2007-11-12T11:44:40Z")

</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  
&nbsp;&nbsp;&nbsp;when 5,6,7: puts 'a'  
&nbsp;&nbsp;&nbsp;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:** ![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:** [12 November 2007 15:18 UTC](https://rubytalk.org/t/case-when-when/42119/3 "2007-11-12T15:18:12Z")

</div>

case foo  
when 1, 2, 10  
&nbsp;&nbsp;then ...  
else  
&nbsp;&nbsp;...  
end

Cheers

robert

> **···**
>
> 2007/11/12, Shuaib Zahda \<shuaib.zahda@gmail.com\>:
> 
> > 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
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Hasan\_Ozgan](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@Hasan\_Ozgan](https://rubytalk.org/u/Hasan_Ozgan)\
**Post date:** [13 November 2007 12:21 UTC](https://rubytalk.org/t/case-when-when/42119/4 "2007-11-13T12:21:06Z")

</div>

case x  
&nbsp;&nbsp;&nbsp;when 5..9: puts 'a' # 5,6,7,8,9  
&nbsp;&nbsp;&nbsp;when 10...14: puts 'b' # 10,11,12,13  
&nbsp;&nbsp;&nbsp;else puts 'c'  
end

# x = 5 then |\> a  
# x = 10 then |\> b  
# x = 14 then |\> c

> **···**
>
> On 11/12/07, Peter Szinek \<peter@rubyrailways.com\> wrote:
> 
> > 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  
> > &nbsp;&nbsp;&nbsp;when 5,6,7: puts 'a'  
> > &nbsp;&nbsp;&nbsp;when 10,11: puts 'b'  
> > end
> > 
> > HTH,  
> > Peter  
> > \_\_\_  
> > [http://www.rubyrailways.com](http://www.rubyrailways.com)  
> > [http://scrubyt.org](http://scrubyt.org)
