我有文本文件t.txt的数字总和,我想计算的所有数字之和在文本文件中 例如何计算的所有文本文件
--- t.txt ---
The rahul jumped in 2 the well. The water was cold at 1 degree Centigrade. There were 3 grip holes on the walls. The well was 17 feet deep.
--- EOF --
总和2 + 1 + 3 + 1 + 7 我的Ruby代码来计算总和
ruby -e "File.read('t.txt').split.inject(0){|mem, obj| mem += obj.to_f}"
,但我没有得到任何答案?
注意'inject'使用块的返回值,分配是多余的。换句话说:你应该在块中使用'mem + ...'而不是'mem + = ...'。 – Stefan