【发布时间】:2018-05-30 20:08:44
【问题描述】:
我有一个包含Enumerable 模块的类,如下所示:
class Paragraph
include Enumerable
attr_accessor :error_totals
def initialize(text, error_totals)
@original = text
@error_totals = error_totals
end
def each
error_totals.each {|category, total| yield category, total }
end
def totals
each.reduce(0) {|t,(category,total)| to += total }
end
end
我收到以下错误,但我不明白为什么:
LocalJumpError:
no block given (yield)
但是,当我执行以下操作时,它会起作用:
def total_errors_per(number_of_words:)
result = 0.0
each {|category, total| result += total.to_f/original.word_count*number_of_words}
result
end
为什么 reduce 会导致这个问题?在调用减少之后,我正在传递一个块。我真的很想通过了解这个问题来了解如何正确使用 Enumerable 模块。
【问题讨论】:
标签: ruby enumerable