Star in case statements

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Star in case statements

    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
    [email]hal9000@hypermetrics.com[/email]


    Hal E. Fulton Guest

  2. Similar Questions and Discussions

    1. M.I'5.Pe rsecution - how a nd wh y di d it star t?
      -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -= how and why did. it start? -= -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- The harassment didn't start by itself,. so...
    2. M-I 5`Persecutio n - h ow and w hy di d it star t?
      -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -= how and why. did it start? -= -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- The harassment didn't start by itself, so...
    3. M`I'5-Per secution . ho w a nd w hy did it star t?
      -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -= how and. why did it start? -= -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- The harassment didn't start by itself, so...
    4. Problem with Star Wars 3 DVD
      Hi all, i've buy the star wars DVD and to play it he run macromedia projector,but after when i do "play movie" .... nothing. the movie don't start...
    5. #23026 [Com]: Make Zend case-sensitive (classes, functions, remove case-insensitive)
      ID: 23026 Comment by: nvivo at mandic dot com dot br Reported By: mfischer@php.net Status: Open Bug Type: ...
  3. #2

    Default Re: Star in case statements

    Hal E. Fulton <hal9000@hypermetrics.com> 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 Guest

  4. #3

    Default Re: Star in case statements

    Dave Brown <dagbrown@lart.ca> 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139