Star in case statements

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

  1. 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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Hal E. Fulton

    Dave Brown Guest

    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
    #3
  4. 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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,196
    mfmehdi
    Oct 20, 2006
  2. Replies:
    6
    Views:
    4,327
    Douglas Sykora
    Feb 11, 2005
  3. Harry George
    Replies:
    6
    Views:
    393
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    754
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    267
    John Crichton
    Jul 12, 2010
Loading...

Share This Page