# Star in case statements

Discussion in 'Ruby' started by Hal E. Fulton, Aug 20, 2003.

1. ### Hal E. FultonGuest

I just noticed something that I either didn't know
or just forgot.

You can use the *array notation to specify values
for the when clause in a case statement:

list1 = [1,2,6,8,9]
list2 = [3,4,5,7,10]
case item
when *list1
puts "in list 1"
when *list2
puts "in list 2"
end

Cool, eh?

Hal

--
Hal Fulton

Hal E. Fulton, Aug 20, 2003

2. ### Martin DeMelloGuest

Hal E. Fulton <> wrote:
> I just noticed something that I either didn't know
> or just forgot.
>
> You can use the *array notation to specify values
> for the when clause in a case statement:

Very neat! I was gratified when I first discovered you could splat a
range too (as in a = *(1..10)).

martin

Martin DeMello, Aug 21, 2003

3. ### Dave BrownGuest

In article <moY0b.1769\$K44.1567@edtnps84>,
Martin DeMello <> wrote:
: Hal E. Fulton <> wrote:
: > I just noticed something that I either didn't know
: > or just forgot.
: >
: > You can use the *array notation to specify values
: > for the when clause in a case statement:
:
: Very neat! I was gratified when I first discovered you could splat a
: range too (as in a = *(1..10)).

This is weird though:

irb(main):001:0> *(1..10)
SyntaxError: compile error
(irb):1: syntax error
from (irb):1
irb(main):002:0> a=*(1..10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):003:0>

I wonder why that does that.

--Dave
--
`I did eventually find a mail-order anvil supplier over the Internet.
I'm not sure how they ship them, but in case they're delivered by air, I
better get my tiny umbrella ready, and keep a sign saying "(yipe)" in my
pocket.' -- James "Kibo" Parry

Dave Brown, Aug 21, 2003
4. ### Martin DeMelloGuest

Dave Brown <> wrote:
>
> This is weird though:
>
> irb(main):001:0> *(1..10)
> SyntaxError: compile error
> (irb):1: syntax error
> from (irb):1
> irb(main):002:0> a=*(1..10)
> => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> irb(main):003:0>
>
> I wonder why that does that.

*(1..10) doesn't expand to [1,2,3,4,5,6,7,8,9,10], it expands to
1,2,3,4,5,6,7,8,9,10. a = 1,2,3,4,5,6,7,8,9,10 does the right thing (see
the multiple assignment section of the pickaxe book). [*(1..10)] works
too.

martin

Martin DeMello, Aug 21, 2003