No superclass when calling super in define_method

  • Why does the following talk: super: no superclass method talk (NoMethodError) error occur talk: super: no superclass method talk (NoMethodError) when I override an existing method?
  • How can I fix this code to call the super method?

Here is an example of the code I'm using

 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") 
+4
source share
1 answer

It does not work because you are overwriting #talk. try it

 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") 
+4
source

All Articles