Skip to content

Instantly share code, notes, and snippets.

@Vaguery
Created October 19, 2010 23:13
Show Gist options
  • Select an option

  • Save Vaguery/635378 to your computer and use it in GitHub Desktop.

Select an option

Save Vaguery/635378 to your computer and use it in GitHub Desktop.
# method for inserting items into an Array without overlapping or nesting them
a = (1..10).to_a
# [1,2,3,4,5,6,7,8,9,10]
where = (10.times.collect {rand(10)}).sort {|a,b| b <=> a}
# e.g., [9, 9, 8, 8, 7, 5, 4, 3, 3, 0]
where.each {|position| a.insert(position,["foo","bar"]).flatten!}
a
# ["foo", "bar", 1, 2, 3, "foo", "bar", "foo", "bar", 4, "foo", "bar", 5, "foo", "bar", 6, 7, "foo", "bar", 8, "foo", "bar", "foo", "bar", 9, "foo", "bar", "foo", "bar", 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment