From: Kevin Olbrich Date: 2006-03-29T00:40:01+09:00 Subject: Re: Regexp gotcha You could just do this... if string =~ /(\w)/ #do something with $1 end anytime you try to match a string to a non-regexp object you get a false, I think. _Kevin On Wednesday, March 29, 2006, at 12:24 AM, Pistos Christou wrote: >Hi, all. I was fixing a bug last night, and discovered some >"gotcha"-like behaviour in the process. Consider: > >irb(main):173:0> s = "my string" >=> "my string" >irb(main):174:0> r1 = /my/ >=> /my/ >irb(main):175:0> r2 = /your/ >=> /your/ >irb(main):176:0> r3 = nil >=> nil >irb(main):177:0> s =~ r1 >=> 0 >irb(main):178:0> s =~ r2 >=> nil >irb(main):179:0> s =~ r3 >=> false > >s =~ r1 .... That's cool, it gives me the index of the match. >s =~ r2 .... That's cool, it tells me there was no match. >s =~ r3 .... Whoa. > >The reason this "got me" is that I had this code: > >match_result = ( some_string =~ some_regexp ) >if match_result != nil > # Assume there was a match >end > >But the problem is... I had an s =~ r3 case because some_regexp was nil, >and so it was entering my if block when I semantically did not want that >to occur. :( > >So now my code is > >if match_result != nil and match_result != false >... >end > >Note also that I can't even use Regexp.last_match != nil in my if block: > >irb(main):224:0> s =~ r2 >=> nil >irb(main):225:0> Regexp.last_match >=> nil >irb(main):226:0> s =~ r3 >=> false >irb(main):227:0> Regexp.last_match >=> nil >irb(main):228:0> s =~ r1 >=> 0 >irb(main):229:0> Regexp.last_match >=> # >irb(main):230:0> s =~ r3 >=> false >irb(main):231:0> Regexp.last_match >=> # > >To be clear: Note how a "nil type" of non-match overwrites last_match, >but a "false type" of non-match doesn't. > >So the question is... why are BOTH nil and false possible return values >of =~ ? Is there some benefit to this? Why not just one or the other? > >I see that this behaviour is [documented][1] but I still feel that this >is unintuitive behaviour when people assume =~ only applies to Regexp >RHS's. > >[1]: http://www.ruby-doc.org/core/classes/String.html#M001453 > >Thanks in advance for any and all clarifications and explanations. > >Pistos > >-- >Posted via http://www.ruby-forum.com/. > -- Posted with http://DevLists.com. Sign up and save your time!