Reverse a hash in Ruby

后端 未结 9 1065
无人共我
无人共我 2021-02-06 23:22

How would I reverse the elements in the hash, keeping the same values and keys, but reversing their order in the hash.

Like so:

{ \"4\" => \"happiness         


        
9条回答
  •  终归单人心
    2021-02-06 23:39

    hash = { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" }
    reversed_hash = Hash[hash.to_a.reverse]
    

提交回复
热议问题