String#include? with several possibilities?

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

string = "The quick brown fox jumps over the lazy dog"
["random", "quick", "another random"].any? { |e| string.include? e} # => true
["random", "foo", "another random"].any? { |e| string.include? e} # => false

···

On [Sat, 21.02.2009 03:24], Aldric Giacomoni wrote:

Is that possible with a built-in method?

--
Dominik Honnef
dominikho@gmx.net

$: 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.

···

On Fri, Feb 20, 2009 at 11:54 PM, Aldric Giacomoni <"aldric[remove]"@trevoke.net> wrote:

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?

Yes, of course!

puts "Yes!" if %w[quick brown lazy].any? {|s| string.include? s}

Kind regards

  robert

···

On 20.02.2009 19:25, Aldric Giacomoni wrote:

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?

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

puts "Yes!" if string =~ pattern

···

On Feb 20, 2009, at 1:51 PM, lasitha wrote:

On Fri, Feb 20, 2009 at 11:54 PM, Aldric Giacomoni > <"aldric[remove]"@trevoke.net> wrote:

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?

lasitha wrote:

···

On Fri, Feb 20, 2009 at 11:54 PM, Aldric Giacomoni > <"aldric[remove]"@trevoke.net> wrote:
  

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.

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

--Aldric

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 :slight_smile:

$: 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.

···

On Sat, Feb 21, 2009 at 1:04 AM, Aldric Giacomoni > lasitha wrote:

$: 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?