Q:

Ruby Regex MatchData Object Captures Method Behaving Oddly

So if you take a Ruby 2.1.2 regex object
reg = /(?!gemspec)gem.*/

and call match on a line like the following:
"gem 'wirble', :group => :development"

You get the following MatchData object:
 #<MatchData "gem 'wirble', :group => :development"> 

If you call
 reg.match("gem 'wirble', :group => :development").to_a
 => ["gem 'wirble', :group => :development"] 

But if you call
reg.match("gem 'wirble', :group => :development").captures
 => [] 

The documentation for MatchData (http://ruby-doc.org/core-2.1.2/MatchData.html#method-i-captures) states that the #captures method should return the same value as the #to_a method on the match object returned from that Regex call. In this instance it doesn't appear that way. Why the discrepancy? Is this a bug?
The post is pretty terse, let me know if you need any other explanation!

A:

This is not a bug. It actually works properly. #captures only returns captured contents which mean the contents that are matched for groups within round brackets (). If you modify your regex to /(?!gemspec)gem(.*)/ (notice the (.*) part), reg.match("gem 'wirble', :group => :development").captures will return [" 'wirble', :group => :development"].

