Skip to content

Instantly share code, notes, and snippets.



Last active Dec 15, 2015
What would you like to do?
Equally distribute several array elements across single array. (Ruby)
#!/usr/bin/env ruby
require 'pp'
a = []
a << [:a, :c, :e]
a << [:b, :f, :g, :h, :i, :j]
a << [:d]
# element coefficients
def get_coefficients array
(1..array.size).map { |i| (i - 0.5) / array.size }
elements = []
coefficients = []
a.each do |e|
elements += e
coefficients += get_coefficients(e)
# Combine all the arrays with their sort index.
combined =
# Extract the values from the original arrays in their new order
r = combined.sort_by { |zipped| zipped[1] }.map { |zipped| zipped[0] }
pp r
# [:b, :a, :f, :g, :d, :c, :h, :i, :e, :j]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.