Change Hash Array keys to strings

来源:互联网 发布:手机淘宝2017旧版 编辑:程序博客网 时间:2024/04/29 01:38

I have this Hash:

{["word"]=>1, ["cat"]=>2, ["tree"]=>1, ["dog"]=>1}

But I want to have this Hash:

{"word"=>1,"cat"=>2,"tree"=>1,"dog"=>1}

Another one:

hash = {["word"]=>1, ["cat"]=>2, ["tree"]=>1, ["dog"]=>1}hash.map { |(k), v| [k, v] }.to_h#=> {"word"=>1, "cat"=>2, "tree"=>1, "dog"=>1}
+1 This is awesome. Any documentation for |(.)| syntax? – BroiSatse 9 hours ago

@BroiSatse that's Ruby's array decomposition feature – Stefan 8 hours ago 

0 0