Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > match with string instead of regexp fails

Reply
Thread Tools

match with string instead of regexp fails

 
 
matt neuburg
Guest
Posts: n/a
 
      10-11-2006
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
 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      10-11-2006
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/cvs...e=text%2Fplain

 
Reply With Quote
 
 
 
 
matt neuburg
Guest
Posts: n/a
 
      10-11-2006
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/cvs...ev=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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57