diff options
author | NAITOH Jun <[email protected]> | 2024-01-13 06:22:32 +0900 |
---|---|---|
committer | Sutou Kouhei <[email protected]> | 2024-01-14 22:27:24 +0900 |
commit | 338eb0065bd81ba8ae8b9402abc94804a24594cc (patch) | |
tree | 12ccd44a5ab58191d511b643484b32999dcedc27 /test/strscan | |
parent | 0610f555ea4f3ba571482f90393fe8c0701cd58a (diff) |
[ruby/strscan] StringScanner#captures: Return nil not "" for
unmached capture
(https://github.com/ruby/strscan/pull/72)
fix https://github.com/ruby/strscan/issues/70
If there is no substring matching the group (s[3]), the behavior is
different.
If there is no substring matching the group, the corresponding element
(s[3]) should be nil.
```
s = StringScanner.new('foobarbaz') #=> #<StringScanner 0/9 @ "fooba...">
s.scan /(foo)(bar)(BAZ)?/ #=> "foobar"
s[0] #=> "foobar"
s[1] #=> "foo"
s[2] #=> "bar"
s[3] #=> nil
s.captures #=> ["foo", "bar", ""]
s.captures.compact #=> ["foo", "bar", ""]
```
```
s = StringScanner.new('foobarbaz') #=> #<StringScanner 0/9 @ "fooba...">
s.scan /(foo)(bar)(BAZ)?/ #=> "foobar"
s[0] #=> "foobar"
s[1] #=> "foo"
s[2] #=> "bar"
s[3] #=> nil
s.captures #=> ["foo", "bar", nil]
s.captures.compact #=> ["foo", "bar"]
```
https://docs.ruby-lang.org/ja/latest/method/MatchData/i/captures.html
```
/(foo)(bar)(BAZ)?/ =~ "foobarbaz" #=> 0
$~.to_a #=> ["foobar", "foo", "bar", nil]
$~.captures #=> ["foo", "bar", nil]
$~.captures.compact #=> ["foo", "bar"]
```
* StringScanner#captures is not yet documented.
https://docs.ruby-lang.org/ja/latest/class/StringScanner.html
https://github.com/ruby/strscan/commit/1fbfdd3c6f
Diffstat (limited to 'test/strscan')
-rw-r--r-- | test/strscan/test_stringscanner.rb | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb index 2fce4c3e74..29626b159f 100644 --- a/test/strscan/test_stringscanner.rb +++ b/test/strscan/test_stringscanner.rb @@ -737,8 +737,8 @@ module StringScannerTests def test_captures s = create_string_scanner("Timestamp: Fri Dec 12 1975 14:39") s.scan("Timestamp: ") - s.scan(/(\w+) (\w+) (\d+) /) - assert_equal(["Fri", "Dec", "12"], s.captures) + s.scan(/(\w+) (\w+) (\d+) (1980)?/) + assert_equal(["Fri", "Dec", "12", nil], s.captures) s.scan(/(\w+) (\w+) (\d+) /) assert_nil(s.captures) end |