0, "two"=>0, "three"=>0, "fou-6ren">
gpt4 book ai didi

ruby - 将数组转换为散列并初始化为一个值

转载 作者:数据小太阳 更新时间:2023-10-29 07:41:16 30 4
gpt4 key购买 nike

转换数组的最佳方法是什么

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com