# Case ... when and arrays (or what was why\_ showing us at rai

**URL:** <https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853>\
**Category:** Ruby\
**Created:** [October 11, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853 "2006-10-11T22:18:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/1 "2006-10-11T22:18:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/2 "2006-10-11T22:25:02Z")

</div>

J2M wrote:

> 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

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/3 "2006-10-11T22:25:02Z")

</div>

On 10/11/06, J2M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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](http://redhanded.hobix.com/bits/wonderOfTheWhenBeFlat.html)

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 10:37pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/4 "2006-10-11T22:37:22Z")

</div>

On 10/11/06, Drew O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 F.

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/5 "2006-10-11T22:39:25Z")

</div>

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

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

Thanks for the answer Robin.

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/6 "2006-10-11T22:51:31Z")

</div>

I like that so much I want more…

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

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/7 "2006-10-11T22:26:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 12, 2006, 1:26am UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/8 "2006-10-12T01:26:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![noagenda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8c91f0/32.png) [@noagenda](https://www.ruby-forum.com/u/noagenda)\
**Post date:** [October 11, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/case-when-and-arrays-or-what-was-why-showing-us-at-rai/71853/9 "2006-10-11T23:41:49Z")

</div>

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
