转换数组的最佳方法是什么
arr = ["one", "two", "three", "four", "five"]
散列的
{"one"=>0, "two"=>0, "three"=>0, "four"=>0, "five"=>0}
我打算稍后用我自己的值填充'0',我现在只需要这个技术。
谢谢。
最佳答案
arr.product([0]).to_h
或版本 < 2.0
Hash[arr.product([0])]
关于ruby - 将数组转换为散列并初始化为一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30497533/