在ruby中查找哈希值数组的总和

时间:2014-11-13 11:41:03

标签: ruby

在ruby抛出错误

中查找散列值数组的总和
parsed_response = [
  {"type"=>"trading", "currency"=>"btc", "amount"=>"1.19782496", "available"=>"1.19782496"},
  {"type"=>"trading", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"deposit", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"deposit", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"exchange", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"exchange", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}
]

parsed_response.map { |h| h["available"].to_f }.sum

这会引发以下错误:

undefined method `sum' for [1.19782496, 0.0, 0.0, 0.0, 0.0, 0.0]:Array (NoMethodError)

我应该如何更改我的代码?

5 个答案:

答案 0 :(得分:1)

因为,你只使用Ruby。你可以试试这个(因为Ruby中的数组没有sum方法):

parsed_response.inject(0){|sum, response| sum = sum+response['available'].to_f }
#=> 1.19782496

如果您使用的是Rails,那么EnumerableArray包含的sum方法可以像parsed_response.map { |h| h["available"].to_f }.sum #=> 1.19782496 那样使用,可以这样使用:

{{1}}

答案 1 :(得分:1)

Enumerable#sum是一种在vanilla Ruby中不存在的方法。相反,它是作为Ruby on Rails Web开发框架的一部分加载的扩展。

在vanilla Ruby中,一个合理的替代品是Enumerable#reduce

parsed_response = [
  {"type"=>"trading", "currency"=>"btc", "amount"=>"1.19782496", "available"=>"1.19782496"},
  {"type"=>"trading", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"deposit", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"deposit", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"exchange", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"exchange", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}
]

parsed_response.map { |h| h["available"].to_f }.reduce(:+)
#=> 1.19782496

答案 2 :(得分:0)

你的问题不清楚。

在积极支持下,应定义sum

require 'active_support/all'
parsed_response = [{"type"=>"trading", "currency"=>"btc", "amount"=>"1.19782496", "available"=>"1.19782496"}, {"type"=>"trading", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"deposit", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"deposit", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"exchange", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"exchange", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}]
parsed_response.map { |h| h["available"].to_f }.sum
# => 1.19782496

如果没有有效支持,sum未定义,您应该使用inject(:+)代替:

parsed_response = [{"type"=>"trading", "currency"=>"btc", "amount"=>"1.19782496", "available"=>"1.19782496"}, {"type"=>"trading", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"deposit", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"deposit", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"exchange", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"}, {"type"=>"exchange", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}]
parsed_response.map { |h| h["available"].to_f }.inject(:+)
# => 1.19782496

如果您忘记to_f转换错误,您将连接所有字符串:

["1.19782496", "0.0", "0.0", "0.0", "0.0", "0.0"].inject(:+)
# => 1.197824960.00.00.00.00.0

答案 3 :(得分:0)

实现这一目标的一种非常简单的方法:

result = 0.0
parsed_response.each do |item|
  result += item["available"].to_f
end

答案 4 :(得分:0)

这有效:

parsed_response = [
  {"type"=>"trading", "currency"=>"btc", "amount"=>"1.19782496", "available"=>"1.19782496"},
  {"type"=>"trading", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"deposit", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"deposit", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"exchange", "currency"=>"btc", "amount"=>"0.0", "available"=>"0.0"},
  {"type"=>"exchange", "currency"=>"usd", "amount"=>"0.0", "available"=>"0.0"}
]

parsed_response.collect { |a| a["available"].to_f }.inject(:+)
#=> 1.19782496