[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: match with string instead of regexp fails

Cameron, Gemma (UK)

10/12/2006 10:53:00 AM


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.
********************************************************************

1 Answer

Jano Svitok

10/12/2006 9:43:00 PM

0

On 10/12/06, Cameron, Gemma (UK) <Gemma.Cameron@baesystems.com> wrote:
>
> 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?!?!

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

str =~ obj => fixnum or nil

Match—<snip> 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
<snip>
(http://ruby-doc.org/core/classes/String.ht...)

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).