String#include? with several possibilities?

A

Aldric Giacomoni

Say I have a string like.. At random...
string = "The quick brown fox jumps over the lazy dog"
I'd like to do something like:
puts "Yes!" if string.include? ('quick', 'brown', 'lazy')
Expected behavior : if -any- of them are included, return true.

Is that possible with a built-in method?

Thanks,
--Aldric
 
L

lasitha

Say I have a string like.. At random...
string = "The quick brown fox jumps over the lazy dog"
I'd like to do something like:
puts "Yes!" if string.include? ('quick', 'brown', 'lazy')
Expected behavior : if -any- of them are included, return true.

Is that possible with a built-in method?

$: irb
01> s = "The quick brown fox jumps over the lazy dog"
--> "The quick brown fox jumps over the lazy dog"
02> s =~ /(quick|brown|fox)/
--> 4
03> s =~ /(not|this|nor|that)/
--> nil

Cheers,
lasitha.
 
A

Aldric Giacomoni

lasitha said:
$: irb
01> s = "The quick brown fox jumps over the lazy dog"
--> "The quick brown fox jumps over the lazy dog"
02> s =~ /(quick|brown|fox)/
--> 4
03> s =~ /(not|this|nor|that)/
--> nil

Cheers,
lasitha.
Oh.. Duh! Must learn to thing with regexp. Thank you! What does the
number mean?

--Aldric
 
G

Gary Wright

pattern = Regexp.union(*%w{quick brown lazy})
string = "The quick brown fox jumps over the lazy dog"

puts "Yes!" if string =~ pattern
 
L

lasitha

$: irb
01> s = "The quick brown fox jumps over the lazy dog"
--> "The quick brown fox jumps over the lazy dog"
02> s =~ /(quick|brown|fox)/
--> 4
03> s =~ /(not|this|nor|that)/
--> nil

Oh.. Duh! Must learn to thing with regexp. Thank you! What does the number
mean?[/QUOTE]

It was a mindshift for me too, no doubt.

If i give you a couple more samples i'm sure you'll figure out what
the number stands for :)

$: irb
01> s = "The quick brown fox jumps over the lazy dog"
--> "The quick brown fox jumps over the lazy dog"
02> s =~ /quick|brown|fox/
--> 4
03> s =~ /brown|fox/
--> 10
04> s =~ /fox/
--> 16

By the way, the parentheses surrounding the union in my previous post
were superfluous.

Cheers,
lasitha.
 

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

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top