Hello to all,
First post in Ruby-Forum. Maybe some Ruby expert could help me.
Does somebody knows how to make all arrays of a hash to have the same
size?
This is, add blank elements to smaller arrays.
I found that for an array of arrays this could be done like this:
array=[[“x”,“y”,“z”], [“w”,“p”], [“q”, “x”, “c”, “v”]]
max_size = array.map(&:size).max
array.map { |a| Array.new(max_size) { |i| a[i] || ‘’ } }
Output: [[“x”, “y”, “z”, “”], [“w”, “p”, “”, “”], [“q”, “x”, “c”,“v”]]
For an hash of arrays I’ve been trying to replicate the code above
without succes.
hash = {“1”=>[“x”,“y”,“z”], “2”=>[“w”,“p”], “3”=>[“q”, “x”, “c”, “v”]}
max_size = hash.values.map(&:size).max
myHash.values.map { |k,v| myHash.new(max_size) { |i| a[i] || ‘’ } }
Asked here too
“DEVShed”
Thanks in advance.