7
  • Why do I get the following error talk: super: no superclass method talk (NoMethodError) when I override a method that already exists?
  • How could I fix this code to call the super method?

Here is the sample 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")
austen
  • 3,314
  • 3
  • 25
  • 26
  • See http://stackoverflow.com/questions/4470108/when-monkey-patching-a-method-can-you-call-the-overridden-method-from-the-new-i/4471202#4471202 – Eric Walker Dec 12 '12 at 19:04

1 Answers1

5

It's not working because you overwrite #talk. Try this

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")
MC2DX
  • 562
  • 1
  • 7
  • 19