Guest User

Untitled

a guest
Jan 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
  2.  
  3. hash1 = {"a" => "b", "d" => "e", "g" => "h"}
  4. hash2 = {"a" => "c", "d" => "f", "g" => "i"}
  5.  
  6. array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
  7.  
  8. hash1 = array.map { |f,m,_| [f,m] }.to_h
  9. #=> {"a"=>"b", "d"=>"e", "g"=>"h"}
  10. hash2 = array.map { |f,_,l| [f,l] }.to_h
  11. #=> {"a"=>"c", "d"=>"f", "g"=>"i"}
  12.  
  13. def doit(arr, i1, i2)
  14. arr.map { |a| [a[i1], a[i2]] }.to_h
  15. end
  16.  
  17. hash1 = doit(array, 0, 1)
  18. #=> {"a"=>"b", "d"=>"e", "g"=>"h"}
  19. hash2 = doit(array, 0, 2)
  20. #=> {"a"=>"c", "d"=>"f", "g"=>"i"}
  21.  
  22. hash2 = array.map { |a| [a[0], a.pop] }.to_h
  23. hash1 = array.to_h
  24.  
  25. k, *v = array.shift.zip(*array)
  26. hash1, hash2 = v.map { |v| k.zip(v).to_h }
  27.  
  28. array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
  29.  
  30. hash1 = {}
  31. hash2 = {}
  32.  
  33. array.each do |k, v1, v2|
  34. hash1[k] = v1
  35. hash2[k] = v2
  36. end
  37.  
  38. hash1 #=> {"a"=>"b", "d"=>"e", "g"=>"h"}
  39. hash2 #=> {"a"=>"c", "d"=>"f", "g"=>"i"}
  40.  
  41. array = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
  42. hashes = {}
  43. array.each do |subarray|
  44. subarray[1..-1].each_with_index do |item, index|
  45. hashes[index] ||= {}
  46. hashes[index][subarray.first] = item
  47. end
  48. end
  49.  
  50. puts hashes.values
Add Comment
Please, Sign In to add comment