开发者

Why isn't this use of inject working?

开发者 https://www.devze.com 2023-03-31 15:02 出处:网络
Why isn\'t this use of inject working? Ruby: p \"STARTING\" notes = notes.find_all{|note| note.date_occurred == date}

Why isn't this use of inject working?

Ruby:

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

Output:

"STARTING"
Array
#<Note id: 82, time_spent: 开发者_如何学C5, created_at: "2011-08-29 00:32:26", updated_at: "2011-08-29 00:32:31", date_occurred: "2011-08-29">

I'm using Rails 3.0.1 and Ruby 1.9.2


You need to pass the initial value for the "memo" to inject, like:

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

精彩评论

暂无评论...
验证码 换一张
取 消