How could I check to see if a word exists in a string, and return false if it doesn't, in ruby?

Like this: puts "yes" if str =~ /do:/i To return a boolean value (from a method, presumably), compare the result of the match to nil : def has_do(str) (str =~ /do:/i)! = nil end Or, if you don’t like the! = nil then you can use!

~ instead of ~ and negate the result: def has_do(str) not str! ~ /do:/i end But I don’t really like double negations ….

Like this: puts "yes" if str =~ /do:/i To return a boolean value (from a method, presumably), compare the result of the match to nil: def has_do(str) (str =~ /do:/i)! = nil end Or, if you don’t like the! = nil then you can use!

~ instead of =~ and negate the result: def has_do(str) not str! ~ /do:/i end But I don’t really like double negations ….

E.g. If str =~ /do: /i puts "yes" end – oxo Apr 4 at 13:18 @oxo Yes of course.In fact, the first code is merely a shorthand of that. – Konrad Rudolph Apr 4 at 13:49 There's also the good old!

Trick to only get a boolean result: >>!(str =~ /do:/i) #=> true and >>!(str =~ /da:/i) #=> false. – Michael Kohl Apr 4 at 14:40.

If all I'm looking for is a case=insensitive substring match I usually use: str. Downcase'do: ' 9 times out of 10 I don't care where in the string the match is, so this is nice and concise. Here's what it looks like in IRB: >> str = "Things to do: eat and sleep." #=> "Things to do: eat and sleep.

" >> str. Downcase'do: ' #=> "do: " >> str. Downcase'foobar' #=> nil Because it returns nil if there is no hit it works in conditionals too.

You could also do this: str.downcase. Include? "Some string".downcase.

Things to do: eat and sleep. ". Index(/do: /i) index returns the position where the match starts, or nil if not found You can learn more about index method here: ruby-doc.org/core/classes/String.html Or about regex here: regular-expressions.info/ruby.html.

In ruby 1.9 you can do like this: str.downcase. Match("do: ") do puts "yes" end It's not exactly what you asked for, but I noticed a comment to another answer. If you don't mind using regular expressions when matching the string, perhaps there is a way to skip the downcase part to get case insensitivity.

For more info, see String#match.

Str. Match(/do: /i) – the Tin Man Apr 4 at 14:07 @theTinMan Oh, and it was even in the first answer as well. Need to think better next time =) – Jakob W Apr 4 at 14:46.

(str =~ /do:/i)! Or, if you don’t like the! = nil then you can use!

9 times out of 10 I don't care where in the string the match is, so this is nice and concise. Because it returns nil if there is no hit it works in conditionals too. "Things to do: eat and sleep.".

It's not exactly what you asked for, but I noticed a comment to another answer. If you don't mind using regular expressions when matching the string, perhaps there is a way to skip the downcase part to get case insensitivity. Terms of service.

Not the answer you're looking for? Or ask your own question.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions