case when when

S

Shuaib Zahda

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
 
R

Robert Klemme

2007/11/12 said:
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
 
H

Hasan Ozgan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top