Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
- hash1 = {"a" => "b", "d" => "e", "g" => "h"}
- hash2 = {"a" => "c", "d" => "f", "g" => "i"}
- array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
- hash1 = array.map { |f,m,_| [f,m] }.to_h
- #=> {"a"=>"b", "d"=>"e", "g"=>"h"}
- hash2 = array.map { |f,_,l| [f,l] }.to_h
- #=> {"a"=>"c", "d"=>"f", "g"=>"i"}
- def doit(arr, i1, i2)
- arr.map { |a| [a[i1], a[i2]] }.to_h
- end
- hash1 = doit(array, 0, 1)
- #=> {"a"=>"b", "d"=>"e", "g"=>"h"}
- hash2 = doit(array, 0, 2)
- #=> {"a"=>"c", "d"=>"f", "g"=>"i"}
- hash2 = array.map { |a| [a[0], a.pop] }.to_h
- hash1 = array.to_h
- k, *v = array.shift.zip(*array)
- hash1, hash2 = v.map { |v| k.zip(v).to_h }
- array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
- hash1 = {}
- hash2 = {}
- array.each do |k, v1, v2|
- hash1[k] = v1
- hash2[k] = v2
- end
- hash1 #=> {"a"=>"b", "d"=>"e", "g"=>"h"}
- hash2 #=> {"a"=>"c", "d"=>"f", "g"=>"i"}
- array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
- hashes = {}
- array.each do |subarray|
- subarray[1..-1].each_with_index do |item, index|
- hashes[index] ||= {}
- hashes[index][subarray.first] = item
- end
- end
- puts hashes.values
Add Comment
Please, Sign In to add comment