Using 'each' to print chunks of an array Working through Day 2 of Ruby in "7 Languages in 7 Weeks" - the answer to the second question seems acceptable, the first one feels quite wrong. Any suggestions?


Print the contents of an array of sixteen numbers, four numbers at a
time, using just each.

group = 0

(1..16).each do |x| 
    print x
    group = group + 1
    if group % 4 == 0
        puts
        group = 0
    end
end

Now, do the same with each_slice in Enumerable (copied directly from http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-each_slice)

(1..16).each_slice(4) {|x| p x}
No need to save group x already saving it.

(1..16).each do |x|
  print x
  puts if x % 4 == 0
end