Skip to content

Instantly share code, notes, and snippets.

@havenwood
Forked from pmarreck/ruby_array_insert_every.rb
Last active December 14, 2015 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/5104692 to your computer and use it in GitHub Desktop.
Save havenwood/5104692 to your computer and use it in GitHub Desktop.
array = (1..12).map &:to_s
class Array
def insert_every n, this
each_slice(n).inject { |r, a| r << this << a }.flatten
end
end
array.insert_every 3, ','
#=> ["1", "2", "3", ",", "4", "5", "6", ",", "7", "8", "9", ",", "10", "11", "12"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment