Re: match with string instead of regexp fails

I thought the syntax was incorrect…

“cat” =~ “a” #=> 1

Fails as it’s looking to match a reg ex not a string.

Should be:

“cat” =~ /a/ #=> 1

Or am I wrong?!?!

Gem


This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.


On 10/12/06, Cameron, Gemma (UK) [email protected] wrote:

Or am I wrong?!?!

Indeed, the example is wrong (judged by current ruby). However the
documentation states that:

str =~ obj => fixnum or nil

Match? If obj is a String, look for it in str (similar to
String#index). Returns the position the match starts, or nil if there
is no match. Otherwise, invokes obj.=~, passing str as an argument.
The default =~ in Object returns false.

“cat o’ 9 tails” =~ ‘\d’ #=> nil

(http://ruby-doc.org/core/classes/String.html#M001453)

The question was why it doesn’t work. The answer: the online
documentation is outdated (I don’t know how the docs in the install
package look like, I haven’t installed 1.8.5 yet).

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