将哈希数组键更改为字符串

tbr*_*oke 0 ruby arrays algorithm hash

我有这个哈希:

{["word"]=>1, ["cat"]=>2, ["tree"]=>1, ["dog"]=>1}
Run Code Online (Sandbox Code Playgroud)

但我希望有这个哈希:

{"word"=>1, "cat"=>2, "tree"=>1, "dog"=>1}
Run Code Online (Sandbox Code Playgroud)

我曾经多次尝试与each_keyjoin,但似乎没有任何工作.

我该怎么做?

Ste*_*fan 5

另一个:

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}
Run Code Online (Sandbox Code Playgroud)