You need to use .scan()to match more than once:
p s.scan(/a/).to_a
And with the grouping, you get one result for a common match and one for each group (when using .match(). Both results are the same in your regular expression.
Some examples:
> /(a)/.match(s).to_a
=> ["a", "a"]
> /(a)+/.match(s).to_a
=> ["aaaa", "a"]
> s.scan(/a/).to_a
=> ["a", "a", "a", "a"]
> s.scan(/(a)/).to_a
=> [["a"], ["a"], ["a"], ["a"]]
> s.scan(/(a)+/).to_a
=> [["a"]]
> s.scan(/(a+)(a)/).to_a
=> [["aaa", "a"]]
> s.scan(/(a)(a)/).to_a
=> [["a", "a"], ["a", "a"]]
source
share