public
Created

  • Download Gist
each_cons.rb
Ruby
1 2 3 4 5 6 7 8 9
# Obviously this could be done with .join("\n"), but let's say we didn't have join...
 
x = [1,2,3,4]
 
# This will yield: 1,2 then 2,3 then 3,4 then 4,nil
(x + [nil]).each_cons(2) do |item, next_item|
puts item
puts "\n" if next_item
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.