如何在阵列中概括第三个位置?
我有一个带有数组的记录:
@arrays = ["41,3,4", "39,2,3"]
我想将每个数组的第三位置总结在一起(4+3)
我尝试过:
@arrays.sum[3]
我需要先迭代数组吗?
谢谢!
I have a record with arrays:
@arrays = ["41,3,4", "39,2,3"]
I want to sum the third position of each of the arrays together (4+3)
I tried:
@arrays.sum[3]
Do I need to iterate the array first?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以给出
一个块告诉它要添加的内容:
You can give
Enumerable#sum
a block to tell it what to add up:通过
,
,获取第3个,将其转换为整数,然后总和。split elements by
,
, grab 3rd, convert it to integer, then sum.