In "Comprehensive Ruby programming course' e-book, I faced a chapter, where author (Jordan Hudgens) describes it like:
“The last thing we are going to try is to return all the integer values from the sentence.”
And he does it like this:
string = "The quick 12 brown foxes jumped over 10 lazy dogs"
p string.to_enum(:scan, /\d+/).map { Regexp.last_match }
And it returns:
=> [#<MatchData "3">, #<MatchData "34">, #<MatchData "23">]
I wonder why / when, this Regexp.last_match
might be used or better to say - why this way is not more efficient than:
p string.to_enum(:scan, /\d+/).map { |i| p i }
This outputs only an array of integers and seems for me as a more efficient way to get those numbers..
Anyone maybe could explain what might was the reasons for author to pick Regesp.last_match
?