Match string from possible strings

J

John Butler

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
 
7

7stud --

John said:
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')
 
K

Kristian Elof Sørensen

Hi

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
 
T

ThoML

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

Tim Hunter

John said:
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"
 
P

Park Heesob

SGksDQotLS0tLSBPcmlnaW5hbCBNZXNzYWdlIC0tLS0tIA0KRnJvbTogIkpvaG4gQnV0bGVyIiA8
am9obm55YnV0bGVyN0BnbWFpbC5jb20+DQpOZXdzZ3JvdXBzOiBjb21wLmxhbmcucnVieQ0KVG86
ICJydWJ5LXRhbGsgTUwiIDxydWJ5LXRhbGtAcnVieS1sYW5nLm9yZz4NClNlbnQ6IEZyaWRheSwg
QXByaWwgMTEsIDIwMDggNzowNSBQTQ0KU3ViamVjdDogTWF0Y2ggc3RyaW5nIGZyb20gcG9zc2li
bGUgc3RyaW5ncw0KDQoNCj4gSGksDQo+IA0KPiBJdmUgbG9va2VkIGFib3V0IGFuZCBmb3VuZCBz
b21lIHdheXMgdG8gZG8gdGhpcyBidXQgdGhlcmUgbXVzdCBiZSBhIDENCj4gbGluZSBxdWljayB3
YXkgaW4gcnVieSB1c2luZyByZWd1bGFyIGV4cHJlc3Npb25zIG9yIHNvbWV0aGluZy4NCj4gDQo+
IEJhc2ljYWxseSBpIGhhdmUgYSBzdHJpbmcgIlRlc3QiIGFuZCBpIHdhbnQgdG8gc2VlIGlmIGl0
IG1hdGNoZXMgMw0KPiBjb25zdGFudHMgdGhhdCBpIGhhdmUgZGVmaW5lZC4gIElmIGl0IGRvZXMg
dGhlbiBpIHdhbnQgdG8gZ28gcm91dGUgYSwNCj4gYW5kIGlmIGl0IGRvZXNudCBpIHdhbnQgdG8g
Z28gcm91dGUgYiwgIHNvOg0KPiANCj4gSWYgIlRlc3QiIChtYXRjaGVzIGVpdGhlciBvZiB0aGUg
Zm9sbG93aW5nKSBzdHJpbmcxLCBzdHJpbmcyLCBzdHJpbmczDQo+IHJvdXRlIGENCj4gZWxzZQ0K
PiByb3V0ZSBiDQo+IGVuZA0KPiANCj4gQW55b25lIGtub3cgdGhlIHF1aWNrIHdheT8NCj4gDQo+
IEpCDQoNCklzIHRoaXMgd2hhdCB5b3Ugd2FudD8NCg0KaWYgW3N0cmluZzEsc3RyaW5nMixzdHJp
bmczXS5pbmNsdWRlPygiVGVzdCIpDQouLi4NCg0KUmVnYXJkcywNClBhcmsgSGVlc29iDQo=
 
J

John Butler

Park said:
Hi,
----- Original Message -----
From: "John Butler" <[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 Heesob

Yes,

This is what i have used,

thanks

JB
 
J

Jasmine Swallow

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top