String#match vs. Regexp#match - confused

O

Old Echo

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
 
A

Adam Shelly

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top