case ... when and arrays (or what was why_ showing us at railsconf europe)

Discussion in 'Ruby' started by J2M, Oct 11, 2006.

  1. J2M

    J2M Guest

    Hi,

    I would like to test a string foo against an array bar in as case...
    when clause and would like to know the best way to do this. I am sure
    why_ did something neat with an * operator, but can't find any
    reference. I could do it long hand, but that just aint rubyish.

    e.g.

    foo = "that"
    bar = %(this that other)
    case foo

    when (some clever way of test for foo in the array bar)
     
    J2M, Oct 11, 2006
    #1
    1. Advertising

  2. Re: case ... when and arrays (or what was why_ showing us at railsconfeurope)

    J2M wrote:
    > Hi,
    >
    > I would like to test a string foo against an array bar in as case...
    > when clause and would like to know the best way to do this. I am sure
    > why_ did something neat with an * operator, but can't find any
    > reference. I could do it long hand, but that just aint rubyish.
    >
    > e.g.
    >
    > foo = "that"
    > bar = %(this that other)


    You don't have an array in bar, just a string. You need to use %w:

    bar = %w(this that other)

    > case foo
    >
    > when (some clever way of test for foo in the array bar)


    when *bar

    which corresponds to:

    when "this", "that", "other"

    > .
    > .
    > else
    >
    > end
    >
    > Anybody got a good suggestion?


    Cheers,
    Robin
     
    Robin Stocker, Oct 11, 2006
    #2
    1. Advertising

  3. J2M

    Jan Svitok Guest

    On 10/11/06, J2M <> wrote:
    > Hi,
    >
    > I would like to test a string foo against an array bar in as case...
    > when clause and would like to know the best way to do this. I am sure
    > why_ did something neat with an * operator, but can't find any
    > reference. I could do it long hand, but that just aint rubyish.
    >
    > e.g.
    >
    > foo = "that"
    > bar = %(this that other)
    > case foo
    >
    > when (some clever way of test for foo in the array bar)
    > .
    > .
    > else
    >
    > end
    >
    > Anybody got a good suggestion?


    when *bar

    http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html
     
    Jan Svitok, Oct 11, 2006
    #3
  4. J2M

    Drew Olson Guest

    Re: case ... when and arrays (or what was why_ showing us at

    If you just want to check if the array contains that string somewhere,
    can you just use:

    if foo =~ array.join(" ")
    blah
    else
    blah2
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    Drew Olson, Oct 11, 2006
    #4
  5. J2M

    Jacob Fugal Guest

    Re: case ... when and arrays (or what was why_ showing us at

    On 10/11/06, Drew Olson <> wrote:
    > If you just want to check if the array contains that string somewhere,
    > can you just use:
    >
    > if foo =~ array.join(" ")
    > blah
    > else
    > blah2
    > end


    Ah, but consider:

    array = [ "foo", "bar", "baz" ]
    foo = "foo bar"
    puts(foo =~ array.join(" ") ? "true" : "false")
    puts(array.include?(foo) ? "true" : "false")
    puts case foo
    when *array: "true"
    else "false"
    end

    produces:

    true
    false
    false

    The semantics are different. Your approach may be valid in some cases,
    I'm not discounting it altogether, but the two approaches do yield
    different results in the edge cases.

    Jacob Fugal
     
    Jacob Fugal, Oct 11, 2006
    #5
  6. J2M

    J2M Guest

    %(....) was over eagre typing on my part.

    Damn I am sure I tried when *bar 2 hours ago!

    Thanks for the answer Robin.
     
    J2M, Oct 11, 2006
    #6
  7. J2M

    J2M Guest

    I like that so much I want more...

    Can you do something equivalent with an if and *bang?
     
    J2M, Oct 11, 2006
    #7
  8. J2M

    Ken Kunz Guest

    To determine if some object is included in a collection:

    collection.include? obj

    e.g.:

    %w(foo bar baz).include? "foo"
    => true

    also, have a look at Enumerable#grep -- it uses === for comparison
    (just like case/when):

    a = [ "bar", 3, 7.5, "baz" ]

    a.grep String
    => ["bar", "baz"]

    a.grep Numeric
    => [3, 7.5]

    a.grep /^b/
    => ["bar", "baz"]

    a.grep "bar"
    => ["bar"]

    a.grep 1..5
    => 3

    -Ken

    J2M wrote:
    > I like that so much I want more...
    >
    > Can you do something equivalent with an if and *bang?
     
    Ken Kunz, Oct 11, 2006
    #8
  9. J2M

    J2M Guest

    Cool thanks Ken, I hadn't really played with grep at all, that and
    *bang have just halved the code I am writing.
     
    J2M, Oct 12, 2006
    #9
    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. James Britt

    Congrats to why_

    James Britt, Jun 20, 2005, in forum: Ruby
    Replies:
    5
    Views:
    130
  2. Chad Fowler
    Replies:
    0
    Views:
    108
    Chad Fowler
    Nov 27, 2005
  3. Replies:
    0
    Views:
    104
  4. Replies:
    0
    Views:
    103
  5. David A. Black
    Replies:
    2
    Views:
    118
    Nathaniel Brown
    Apr 4, 2007
Loading...

Share This Page