我有一个名为Note
的类,其中包括一个名为time_spent
的实例变量.我希望能够做到这样:
current_user.notes.inject{|total_time_spent,note| total_time_spent + note.time_spent}
这是不是可以在Enumerable module%的范围内混合?我知道你应该在类中添加include Enumerable
,然后定义一个each
方法,但是每个方法应该是类还是实例方法?each
方法中有什么?
我使用的是Ruby 1.9.2