talk: super: no superclass method talk (NoMethodError)
?以下是我正在使用的示例代码
class Foo
def talk(who, what, where)
p "#{who} is #{what} at #{where}"
end
end
Foo.new.talk("monster", "jumping", "home")
class Foo
define_method(:talk) do |*params|
super(*params)
end
end
Foo.new.talk("monster", "jumping", "home")
答案 0 :(得分:4)
它无效,因为你覆盖了#talk。试试这个
class Foo
def talk(who, what, where)
p "#{who} is #{what} at #{where}"
end
end
Foo.new.talk("monster", "jumping", "home")
class Bar < Foo
define_method(:talk) do |*params|
super(*params)
end
end
Bar.new.talk("monster", "table", "home")