带有索引的 Ruby `each_with_object` [英] Ruby `each_with_object` with index
本文介绍了带有索引的 Ruby `each_with_object`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用index
来做a.each_with_object
,比这更好:
I would like to do a.each_with_object
with index
, in a better way than this:
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
的情况下做到这一点?
Is there a way to do this without v,i = arr
?
推荐答案
代替
|arr, hash|
你可以做到
|(v, i), hash|
这篇关于带有索引的 Ruby `each_with_object`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文