Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > case ... when and arrays (or what was why_ showing us at railsconf europe)

Reply
Thread Tools

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

 
 
J2M
Guest
Posts: n/a
 
      10-11-2006
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)
 
Reply With Quote
 
 
 
 
Robin Stocker
Guest
Posts: n/a
 
      10-11-2006
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

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      10-11-2006
On 10/11/06, J2M <(E-Mail Removed)> 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/wond...henBeFlat.html

 
Reply With Quote
 
Drew Olson
Guest
Posts: n/a
 
      10-11-2006
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/.

 
Reply With Quote
 
Jacob Fugal
Guest
Posts: n/a
 
      10-11-2006
On 10/11/06, Drew Olson <(E-Mail Removed)> 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

 
Reply With Quote
 
J2M
Guest
Posts: n/a
 
      10-11-2006
%(....) was over eagre typing on my part.

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

Thanks for the answer Robin.


 
Reply With Quote
 
J2M
Guest
Posts: n/a
 
      10-11-2006
I like that so much I want more...

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


 
Reply With Quote
 
Ken Kunz
Guest
Posts: n/a
 
      10-11-2006
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?


 
Reply With Quote
 
J2M
Guest
Posts: n/a
 
      10-11-2006
Cool thanks Ken, I hadn't really played with grep at all, that and
*bang have just halved the code I am writing.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
RailsConf 2005 podcasts Bryan W. Ruby 3 02-21-2006 04:10 PM
[ANN] RailsConf 2006, June 22-25, Chicago IL Chad Fowler Ruby 0 11-27-2005 06:59 PM
Congrats to why_ James Britt Ruby 5 06-21-2005 01:07 AM



Advertisments