Mega Code Archive

 
Categories / Ruby / Hash
 

Associate an array converted from a hash

h = { :a => 1, :b => 2}   # Start with a hash a = h.to_a                # => [[:b,2], [:a,1]]: associative array a.assoc(:a)               # => [:a,1]: subarray for key :a a.assoc(:b).last          # => 2: value for key :b a.rassoc(1)               # => [:a,1]: subarray for value 1 a.rassoc(2).first         # => :b: key for value 2 a.assoc(:c)               # => nil a.transpose               # => [[:a, :b], [1, 2]]: swap rows and cols