Match string from possible strings

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

···

--
Posted via http://www.ruby-forum.com/.

John Butler 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')

···

--
Posted via http://www.ruby-forum.com/\.

Hi

···

On Fri, 11 Apr 2008 19:05:47 +0900, John Butler <johnnybutler7@gmail.com> wrote:

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?

This looks like a potential Ruby Quiz challenge.

    Kristian

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 =~
- ...

John Butler wrote:

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

What's wrong with

if string1 == "Test" || string2 == "Test" || string3 == "Test"

···

--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html

Hi,

···

----- Original Message -----
From: "John Butler" <johnnybutler7@gmail.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Friday, April 11, 2008 7:05 PM
Subject: Match string from possible strings

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

Is this what you want?

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

Regards,
Park Heesob

Hi,

I am currently looking for Ruby on Rails Web Developers 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
j.swallow@preferred-it.com

···

--
Posted via http://www.ruby-forum.com/.

Park Heesob wrote:

Hi,
From: "John Butler" <johnnybutler7@gmail.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
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 Heesob

Yes,

This is what i have used,

thanks

JB

···

----- Original Message -----

--
Posted via http://www.ruby-forum.com/\.

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.

···

-----Original Message-----
From: list-bounce@example.com [mailto:list-bounce@example.com] On Behalf
Of John Butler
Sent: Friday, April 11, 2008 8:44 AM
To: ruby-talk ML
Subject: Re: Match string from possible strings

Park Heesob wrote:

Hi,
----- Original Message -----
From: "John Butler" <johnnybutler7@gmail.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
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 Heesob

Yes,

This is what i have used,

thanks

JB

--
Posted via http://www.ruby-forum.com/\.