match with string instead of regexp fails

Discussion in 'Ruby' started by matt neuburg, Oct 11, 2006.

  1. matt neuburg

    matt neuburg Guest

    According to the 1.8.5 core API:

    str =~ obj => fixnum or nil

    Match. If obj is a Regexp, use it as a pattern to match against str. 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.

    So I expect "cat" =~ "a" #=> 1

    But:

    irb(main):013:0> RUBY_VERSION
    => "1.8.5"
    irb(main):014:0> "cat" =~ "a"
    TypeError: type mismatch: String given
    from (irb):14:in `=~'
    from (irb):14
    from :0

    An undocumented change? Sorry if this has come up before, I did try to
    find out. Thx - m.


    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
     
    matt neuburg, Oct 11, 2006
    #1
    1. Advertising

  2. matt neuburg

    Jan Svitok Guest

    On 10/11/06, matt neuburg <> wrote:
    > According to the 1.8.5 core API:
    >
    > str =~ obj => fixnum or nil
    >
    > Match. If obj is a Regexp, use it as a pattern to match against str. 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.
    >
    > So I expect "cat" =~ "a" #=> 1
    >
    > But:
    >
    > irb(main):013:0> RUBY_VERSION
    > => "1.8.5"
    > irb(main):014:0> "cat" =~ "a"
    > TypeError: type mismatch: String given
    > from (irb):14:in `=~'
    > from (irb):14
    > from :0
    >
    > An undocumented change? Sorry if this has come up before, I did try to
    > find out. Thx - m.


    In the CVS [1] there's a new documentation omitting the 'string'
    clause, so I suppose it's a documentation error.

    If you know a bit of C you can see for yourself.

    From the CVS history - the TypeError was added around 1.8.2, so it's
    there pretty long.

    The doc was fixed between 1.8.5-pre1 and 1.8.5-pre2. Form that it
    seems that the docs on ruby-doc are rather old, and not exactly 1.8.5
    ones as they say...

    [1] http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/string.c?rev=1.182.2.53;content-type=text/plain
     
    Jan Svitok, Oct 11, 2006
    #2
    1. Advertising

  3. matt neuburg

    matt neuburg Guest

    Jan Svitok <> wrote:

    > On 10/11/06, matt neuburg <> wrote:
    > > According to the 1.8.5 core API:
    > >
    > > str =~ obj => fixnum or nil
    > >
    > > Match. If obj is a Regexp, use it as a pattern to match against str. 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.
    > >
    > > So I expect "cat" =~ "a" #=> 1
    > >
    > > But:
    > >
    > > irb(main):013:0> RUBY_VERSION
    > > => "1.8.5"
    > > irb(main):014:0> "cat" =~ "a"
    > > TypeError: type mismatch: String given
    > > from (irb):14:in `=~'
    > > from (irb):14
    > > from :0
    > >
    > > An undocumented change? Sorry if this has come up before, I did try to
    > > find out. Thx - m.

    >
    > In the CVS [1] there's a new documentation omitting the 'string'
    > clause, so I suppose it's a documentation error.
    >
    > If you know a bit of C you can see for yourself.
    >
    > From the CVS history - the TypeError was added around 1.8.2, so it's
    > there pretty long.
    >
    > The doc was fixed between 1.8.5-pre1 and 1.8.5-pre2. Form that it
    > seems that the docs on ruby-doc are rather old, and not exactly 1.8.5
    > ones as they say...
    >
    > [1] http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/string.c?rev=1.182.2.53;
    > content-type=text%2Fplain


    Excellent, thanks. m.


    --
    matt neuburg, phd = , http://www.tidbits.com/matt/
    Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
    AppleScript - http://www.amazon.com/gp/product/0596102119
    Read TidBITS! It's free and smart. http://www.tidbits.com
     
    matt neuburg, Oct 11, 2006
    #3
    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. John Gordon
    Replies:
    13
    Views:
    498
    Ian Kelly
    Dec 20, 2011
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    510
    Mikel Lindsaar
    Mar 31, 2008
  3. Old Echo
    Replies:
    1
    Views:
    187
    Adam Shelly
    Sep 4, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    381
    7stud --
    Aug 21, 2009
  5. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page