小编Rob*_*mas的帖子

Sum of first part of string in an array of strings

I have an array of strings from which I need to extract the first words, convert them to integers and get the their sum.

Example:

["5 Apple", "5 Orange", "15 Grapes"]
Run Code Online (Sandbox Code Playgroud)

Expected output => 25

My attempt:

["5","5","15"].map(&:to_i).sum
Run Code Online (Sandbox Code Playgroud)

ruby arrays string

5
推荐指数
2
解决办法
110
查看次数

标签 统计

arrays ×1

ruby ×1

string ×1