austen
austen

Reputation: 3314

No superclass method when calling super in define_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")

Upvotes: 7

Views: 10102

Answers (1)

MC2DX
MC2DX

Reputation: 572

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")

Upvotes: 5

Related Questions