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

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

Hi

On Fri, 11 Apr 2008 19:05:47 +0900, John B.
[email protected]
wrote:

route a
else
route b
end

Anyone know the quick way?

This looks like a potential Ruby Q. 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 =~

Hi,
----- Original Message -----
From: “John B.” [email protected]
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” [email protected]
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.

Park H. wrote:

Hi,
----- Original Message -----
From: “John B.” [email protected]
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” [email protected]
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

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
[email protected]

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”

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs