[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String#match vs. Regexp#match - confused

Old Echo

9/4/2008 5:50:00 PM

Hello everyone,
I'm very puzzled by the following:

a = ".*me"
b = /.*me/
test = "dont_ignore.me.fixture"

a.match(test)
=> nil
b.match(test)
=> #<MatchData:0x350fe8>

Huh? When looking through the ri documentation for the "match" method on
the String class, it says it take the contents of the String and
converts it to a regexp before doing a match.

So...how come the two matches do not return the same result? And even
more importantly, how can I make sure that they do?

Thank you,
Sebastian
--
Posted via http://www.ruby-....

2 Answers

Adam Shelly

9/4/2008 6:11:00 PM

0

On 9/4/08, Old Echo <kodama@bluexpanse.net> wrote:
> Hello everyone,
> I'm very puzzled by the following:
>
> a = ".*me"
> b = /.*me/
> test = "dont_ignore.me.fixture"
>
> a.match(test)
> => nil
> b.match(test)
> => #<MatchData:0x350fe8>
>
> Huh? When looking through the ri documentation for the "match" method on
> the String class, it says it take the contents of the String and
> converts it to a regexp before doing a match.

test.match a
=> #<MatchData:0x283a534>

The argument is converted to a regexp, not the original String.

-Adam

Old Echo

9/4/2008 8:58:00 PM

0

Thanks Adam! Yeah...turns out I didn't read the ri docs carefully
enough.

On the String class, #match takes a regex and compares the original
string to that. On the Regexp class, #match takes a string and compares
the pattern to that string.



--
Posted via http://www.ruby-....