Nuby - NEW case/when question

Discussion in 'Ruby' started by cremes.devlist@mac.com, Mar 1, 2006.

  1. Guest

    I have a new question regarding _why's post [1].

    [1] http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html

    In the comments he explains that */asterisk is now being used as a
    replacement for the method #to_a. But I don't see how that's true.
    Let's look at an example.

    irb(main):001:0> @r1 = (0..10)
    => 0..10
    irb(main):002:0> @r2 = (11..20)
    => 11..20
    irb(main):003:0> def foo a
    irb(main):004:1> case a
    irb(main):005:2> when *@r1
    irb(main):006:2> puts "#{a} in range r1"
    irb(main):007:2> when @r2.to_a
    irb(main):008:2> puts "#{a} in range r2"
    irb(main):009:2> else
    irb(main):010:2* puts "#{a} NOT in any defined range"
    irb(main):011:2> end
    irb(main):012:1> end
    => nil
    irb(main):013:0> foo 3
    3 in range r1
    => nil
    irb(main):014:0> foo 12
    12 NOT in any defined range
    => nil

    If * is really equivalent to #to_a in this case, then the second
    "when" test should succeed when passed argument 12, right? As can be
    seen from the output, it triggers the else clause.

    I looked up * in the 2nd Ed PickAxe and it doesn't cover this
    functionality at all. It talks about * being used in patterns and in
    coalescing method argument lists.
    , Mar 1, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > I have a new question regarding _why's post [1].
    >
    > [1] http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html
    >
    > In the comments he explains that */asterisk is now being used as a
    > replacement for the method #to_a. But I don't see how that's true.


    In the comment, he's talking about something different, not case/when
    related. (Using the splat with array construction instead of using
    Object#to_a to convert a non-array object to a single-element array.)

    In case/when, it's instead of if ... include?.

    case name
    when *board_members
    when *historians

    vs.

    if board_members.include?(name)
    elsif historians.include?(name)
    ....

    Cheers,
    Dave
    , Mar 1, 2006
    #2
    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. Van Jacques

    nuby question re a method

    Van Jacques, Dec 10, 2003, in forum: Ruby
    Replies:
    0
    Views:
    85
    Van Jacques
    Dec 10, 2003
  2. Boris \BXS\ Schulz

    nuby question: f.rename(x,y) does not work

    Boris \BXS\ Schulz, Jan 3, 2004, in forum: Ruby
    Replies:
    4
    Views:
    93
    Boris \BXS\ Schulz
    Jan 3, 2004
  3. Harry
    Replies:
    2
    Views:
    99
    Samuel Kvarnbrink
    Jan 26, 2004
  4. Edwin Eyan Moragas

    nuby question: question marks in method names

    Edwin Eyan Moragas, Aug 30, 2004, in forum: Ruby
    Replies:
    0
    Views:
    138
    Edwin Eyan Moragas
    Aug 30, 2004
  5. Chinna Karuppan

    class::new :: --> NUBY

    Chinna Karuppan, Mar 19, 2008, in forum: Ruby
    Replies:
    4
    Views:
    102
    Ken Bloom
    Mar 19, 2008
Loading...

Share This Page