Bugs item #4361, was opened at 2006-05-04 21:52
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=1698&aid=4361&group_id=426

Category: Core
Group: None
Status: Open
Resolution: None
Priority: 3
Submitted By: Mitchell Charity (mcharity)
Assigned to: Nobody (None)
Summary: StringScanner#scan() fails on zero-length matches at eos

Initial Comment:
ruby 1.8.4 (2005-12-24) [x86_64-linux]
ruby 1.9.0 (2006-04-08) [x86_64-linux]
ruby 1.9.0 (2006-05-01) [x86_64-linux]

StringScanner's scan() incorrectly returns nil for successful zero-length matches at eos.

 StringScanner.new("").scan(//)
 => nil

Only at eos.  This
 StringScanner.new("x").scan(//)
 => ""
works correctly.

The documentation says scan() "Tries to match with pattern at the current position. If there”Ēs a match, the scanner advances the "scan pointer" and returns the matched string. Otherwise, the scanner returns nil.".  Instead, if there's a (necessarily zero-length) match at eos, you get nil.  Oops.

To get the documented and desirable behavior, one currently needs to write
  ss.scan(re) || (ss.eos? && "" =~ re && "")
instead of simply
  ss.scan(re)
whenever there is a chance the re might do a zero-length match.
Which is unfortunate, and a source of bugs.

Example test:
% ruby -r strscan -e 'p StringScanner.new("").scan(//)'


----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=1698&aid=4361&group_id=426