[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

case when when

Shuaib Zahda

11/12/2007 11:38:00 AM

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

3 Answers

Peter Szinek

11/12/2007 11:45:00 AM

0

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.rubyra...
http://s...




Robert Klemme

11/12/2007 3:18:00 PM

0

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

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

Cheers

robert

--
use.inject do |as, often| as.you_can - without end

Hasan Ozgan

11/13/2007 12:21:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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


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
> when 5,6,7: puts 'a'
> when 10,11: puts 'b'
> end
>
> HTH,
> Peter
> ___
> http://www.rubyra...
> http://s...
>
>
>
>
>