为什么不使用注入工作?

时间:2011-08-29 00:47:06

标签: ruby-on-rails ruby

为什么inject使用不起作用?

红宝石:

p "STARTING"
notes = notes.find_all{|note| note.date_occurred == date}
p notes.class
p notes.inject{|sum, note| sum + note.time_spent }

输出:

"STARTING"
Array
#<Note id: 82, time_spent: 5, created_at: "2011-08-29 00:32:26", updated_at: "2011-08-29 00:32:31", date_occurred: "2011-08-29">

我正在使用Rails 3.0.1和Ruby 1.9.2

1 个答案:

答案 0 :(得分:3)

您需要将“备忘录”的初始值传递给inject,例如:

notes.inject(0) {|sum, note| sum + note.time_spent }