I am coming across the MatchData#captures method in Ruby for the first time and wanted to ask if someone could explain it a bit more for me. The Ruby docs say:
Returns the array of captures; equivalent to
mtch.to_a[1..-1].
However I am currently running a regular expression on a long string and it seems to be returning me the last item evaluated? Does this make sense?
this is the string:
431cdb7b1ad8183a1434b6d1a407731fac0ea61b8d720d733fefaa77f063df8e vidcoder [23/May/2012:01:17:16 +0000] 76.78.212.49 - B24DEA4883A9FF95 REST.GET.OBJECT accounts/6/videos/xboxcCFC/video.mp4 "GET /accounts/6/videos/xboxcCFC/video.mp4 HTTP/1.1" 206 - 2 697898511 56 56 "-" "Apple Mac OS X v10.6.8 CoreMedia v1.0.0.10K549" -
this is the regex:
line.match(%r{^.*\s+HTTP.*\s+-\s+(\d+)\s+}).captures
it returns in this case the number 2
Since you only specified one capturing group in your regular expression, it only returns it (as a one-element array containing the “2” string, which is not the same as returning the “2” string directly):
If you try to capture more elements, your array will contains more elements, just as indicated in the documentation: