Ruby invert hash

14.55 Muhamad Akbar Bin Widayat 0 Comments

Hi all,

This is how to invert hash manually.

path_names = {:glass=>"gelas", :metals=>"", :other=>"lainnya", :papers=>"kertas", :plastics=>"plastik"}
new_names = path_names.inject({}) { |h, (k, v)| h[v] = k; h }


The result:

{"gelas"=>:glass, "lainnya"=>:other, "kertas"=>:papers, "plastik"=>:plastics}


0 komentar:

terimakasih komentarnya