1

What is the difference between ruby's StringScanner#post_match and StringScanner#rest?

scanner = StringScanner.new('Say hello to...')
scanner.scan(/\w+/) #=> 'Say'
scanner.scan(/\s+/) #=> ' '
scanner.rest        #=> 'hello to...'
scanner.post_match  #=> 'hello to...'
scanner.rest.class  #=> String
scanner.post_match.class #=> String

I don't see a difference. It seems like both return a string containing everything after the match.

4

1 回答 1

2

还没有比赛的时候有区别。

require 'strscan'

scanner = StringScanner.new('Say hello to...')
scanner.rest        # => "Say hello to..."
scanner.post_match  # => nil

如果没有匹配 -post_match自然返回 nil(因为,你知道,没有什么可阅读的帖子)。

于 2013-11-08T15:55:25.077 回答