I am new to Ruby and has always used String.scan
to search for the first occurrence of a number. It is kind of strange that the returned value is in nested arra
Short answer: scan
will return all matches. This doesn't make it superior, because if you only want the first match, str.match[2]
reads much nicer than str.scan[0][1]
.
ruby-1.9.2-p290 :002 > 'a 1-night stay, a 2-night stay'.scan(/(a )?(\d*)[- ]night/i).to_a
=> [["a ", "1"], ["a ", "2"]]
ruby-1.9.2-p290 :004 > 'a 1-night stay, a 2-night stay'.match(/(a )?(\d*)[- ]night/i).to_a
=> ["a 1-night", "a ", "1"]
Previous answers state that scan will return every match from the string the method is called on but this is incorrect.
Scan keeps track of an index and continues looking for subsequent matches after the last character of the previous match.
string = 'xoxoxo'
p string.scan('xo') # => ['xo' 'xo' 'xo' ]
# so far so good but...
p string.scan('xox') # => ['xox']
# if this retured EVERY instance of 'xox' it would include a substring
# starting at indices 0 and 2 but only one match is found
#scan
returns everything that the Regex matches.
#match
returns the first match as a MatchData object, which contains data held by special variables like $&
(what was matched by the Regex; that's what's mapping to index 0), $1
(match 1), $2
, et al.