String#match vs. Regexp#match - confused

Discussion in 'Ruby' started by Old Echo, Sep 4, 2008.

  1. Old Echo

    Old Echo Guest

    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-forum.com/.
    Old Echo, Sep 4, 2008
    #1
    1. Advertising

  2. Old Echo

    Adam Shelly Guest

    On 9/4/08, Old Echo <> 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
    Adam Shelly, Sep 4, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    19,684
  2. Flyzone
    Replies:
    11
    Views:
    480
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    482
    Mikel Lindsaar
    Mar 31, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    359
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    190
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page