我想要做a.each_with_object
with index
,以比这更好的方式:
a = %w[a b c]
a.each.with_index.each_with_object({}) { |arr, hash|
v,i = arr
puts "i is: #{i}, v is #{v}"
}
i is: 0, v is a
i is: 1, v is b
i is: 2, v is c
=> {}
有没有办法做到这一点而不需要v,i = arr
?
在你的例子中.each.with_index
是多余的。我找到了这个解决方案:
['a', 'b', 'c'].each_with_object({}).with_index do |(el, acc), index|
acc[index] = el
end
# => {0=>"a", 1=>"b", 2=>"c"}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)