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

J

J2M

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)
 
R

Robin Stocker

J2M said:
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
 
J

Jan Svitok

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
 
D

Drew Olson

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
 
J

Jacob Fugal

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
 
J

J2M

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

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

Thanks for the answer Robin.
 
J

J2M

I like that so much I want more...

Can you do something equivalent with an if and *bang?
 
K

Ken Kunz

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
 
J

J2M

Cool thanks Ken, I hadn't really played with grep at all, that and
*bang have just halved the code I am writing.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top