Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No superclass method when calling super in define_method

Tags:

ruby

  • 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")
like image 366
austen Avatar asked Dec 12 '12 18:12

austen


1 Answers

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")
like image 74
MC2DX Avatar answered Oct 21 '22 06:10

MC2DX