You can access the first and last elements of a range by the corresponding first
and last
methods. inject(:+)
sums up the partial distances of all the ranges belonging to the same group.
a.map { |ranges| ranges.map { |range| range.last - range.first }.inject(:+) }
=> [19, 8, 5]
Or, even shorter, as suggested by tokland using Ruby 2.0:
a.map { |ranges| ranges.map(&:size).reduce(0, :+) }