Ruby on rails 为什么我不能用重载方法在define_方法中调用super?
当我运行下面的代码时,它会引发错误: 不支持从define_method()定义的方法传递super的隐式参数。明确指定所有参数。(运行时错误) 我不确定是什么问题Ruby on rails 为什么我不能用重载方法在define_方法中调用super?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,ruby-on-rails-3.1,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,Ruby On Rails 3.1,当我运行下面的代码时,它会引发错误: 不支持从define_method()定义的方法传递super的隐式参数。明确指定所有参数。(运行时错误) 我不确定是什么问题 class Result def total(*scores) percentage_calculation(*scores) end private def percentage_calculation(*scores) puts "Calculation for #{scores.inspect
class Result
def total(*scores)
percentage_calculation(*scores)
end
private
def percentage_calculation(*scores)
puts "Calculation for #{scores.inspect}"
scores.inject {|sum, n| sum + n } * (100.0/80.0)
end
end
def mem_result(obj, method)
anon = class << obj; self; end
anon.class_eval do
mem ||= {}
define_method(method) do |*args|
if mem.has_key?(args)
mem[args]
else
mem[args] = super
end
end
end
end
r = Result.new
mem_result(r, :total)
puts r.total(5,10,10,10,10,10,10,10)
puts r.total(5,10,10,10,10,10,10,10)
puts r.total(10,10,10,10,10,10,10,10)
puts r.total(10,10,10,10,10,10,10,10)
类结果
def总分(*分)
百分比计算(*分数)
结束
私有的
def百分比计算(*分数)
将“计算#{scores.inspect}”
分数.inject{| sum,n | sum+n}*(100.0/80.0)
结束
结束
def mem_结果(obj,方法)
anon=class错误消息非常具有描述性。在define_method
块内调用super
时,需要显式地将参数传递给它:
mem[args] = super(*args)