Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Star in case statements

Reply
Thread Tools

Star in case statements

 
 
Hal E. Fulton
Guest
Posts: n/a
 
      08-20-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      08-21-2003
Hal E. Fulton <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Dave Brown
Guest
Posts: n/a
 
      08-21-2003
In article <moY0b.1769$K44.1567@edtnps84>,
Martin DeMello <(E-Mail Removed)> wrote:
: Hal E. Fulton <(E-Mail Removed)> 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
 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      08-21-2003
Dave Brown <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM
Re: Sound problems on DVDs (was Re: Star Wars digression was Re: The Myths of Gaming) (was: Star Wars digression was Re: The Myths of Gaming) Terry Austin DVD Video 0 12-02-2003 03:21 AM



Advertisments