# Match string from possible strings

**URL:** <https://www.ruby-forum.com/t/match-string-from-possible-strings/134495>\
**Category:** Ruby\
**Created:** [April 11, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495 "2008-04-11T12:05:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/1 "2008-04-11T12:05:46Z")

</div>

Hi,

Ive looked about and found some ways to do this but there must be a 1  
line quick way in ruby using regular expressions or something.

Basically i have a string “Test” and i want to see if it matches 3  
constants that i have defined. If it does then i want to go route a,  
and if it doesnt i want to go route b, so:

If “Test” (matches either of the following) string1, string2, string3  
route a  
else  
route b  
end

Anyone know the quick way?

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/2 "2008-04-11T12:20:58Z")

</div>

John B. wrote:

> Hi,  
> If “Test” (matches either of the following) string1, string2, string3  
> route a  
> else  
> route b  
> end
> 
> Anyone know the quick way?
> 
> JB

if [string1, string2, string3].include?(‘Test’)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/3 "2008-04-11T12:22:51Z")

</div>

Hi

On Fri, 11 Apr 2008 19:05:47 +0900, John B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> route a  
> else  
> route b  
> end
> 
> Anyone know the quick way?

This looks like a potential Ruby Q. challenge.

```
Kristian

```

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/4 "2008-04-11T13:10:36Z")

</div>

> If “Test” (matches either of the following) string1, string2, string3

Does “match” mean “is equal” or “match a regexp”?

If it is “is equal”

- use include?
- use case

if it means regexp matching

- use […].grep.empty? for no match
- build a single regexp to match all 3 constants and use =~
- …

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/5 "2008-04-11T14:42:33Z")

</div>

Hi,  
----- Original Message -----  
From: “John B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Newsgroups: comp.lang.ruby  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Friday, April 11, 2008 7:05 PM  
Subject: Match string from possible strings

> route a  
> else  
> route b  
> end
> 
> Anyone know the quick way?
> 
> JB

Is this what you want?

if [string1,string2,string3].include?(“Test”)  
…

Regards,  
Park H.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 2:43pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/6 "2008-04-11T14:43:38Z")

</div>

Park H. wrote:

> Hi,  
> ----- Original Message -----  
> From: “John B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Newsgroups: comp.lang.ruby  
> To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Sent: Friday, April 11, 2008 7:05 PM  
> Subject: Match string from possible strings
> 
> > route a  
> > else  
> > route b  
> > end
> > 
> > Anyone know the quick way?
> > 
> > JB
> 
> Is this what you want?
> 
> if [string1,string2,string3].include?(“Test”)  
> …
> 
> Regards,  
> Park H.

Yes,

This is what i have used,

thanks

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 3:04pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/7 "2008-04-11T15:04:20Z")

</div>

Hi,

I am currently looking for Ruby on Rails Web D.s for a permanent  
and a contract opportunity based in London. If you are interested in  
this opportunity, please feel free to contact me. Alternatively, if this  
is not of interest to you but you have any friends or colleagues who are  
looking for either Ruby on Rails work or indeed any other roles within  
the IT field, please feel free to pass their details on to me or pass my  
details to them.

Kind Regards,  
Jasmine  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 11, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/8 "2008-04-11T13:19:26Z")

</div>

John B. wrote:

> route a  
> else  
> route b  
> end
> 
> Anyone know the quick way?
> 
> JB

What’s wrong with

if string1 == “Test” || string2 == “Test” || string3 == “Test”

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 15, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/match-string-from-possible-strings/134495/9 "2008-04-15T17:25:55Z")

</div>

Also, using the facets gem you can turn “include?” around using “in?”,  
which might have a more intuitive feel.

require ‘facets’

> a = “Test”  
> arr = %w(Test Toast Tryst)  
> arrgh = %w(Toast Tryst)  
> a.in? arr  
> =\>true  
> a.in? arrgh  
> =\>false

I saw a presentation on facets @ the Hartford rug a few weeks ago and I  
thoroughly enjoy this gem now.
