6

I noticed for the class definition, if I open up the class MyClass, and add something in between without overwrite I still got the original method which defined earlier. The new statements added augment the existing one.

But as to the method definition, I still want the same behavior as the class definition, but it seems when I open up the def my_method, the exiting statements within the def and end is overwritten, I need to rewrite that again.

So is there any way to make the method definition behave the same as definition, something like super, but not necessarily is the sub-class?

emboss
  • 38,880
  • 7
  • 101
  • 108
mko
  • 21,334
  • 49
  • 130
  • 191
  • you can store the old method in a variable – Karoly Horvath Aug 06 '11 at 12:18
  • This is largely a duplicate of [When monkey patching a method, can you call the overridden method from the new implementation](http://StackOverflow.Com/questions/4470108/when-monkey-patching-a-method-can-you-call-the-overridden-method-from-the-new-im/4471202/#4471202). – Jörg W Mittag Aug 06 '11 at 14:40

1 Answers1

10

I suppose you are looking for alias_method:

class A
  alias_method :old_func, :func

  def func
    old_func # similar to calling 'super'
    # do other stuff
  end
end
emboss
  • 38,880
  • 7
  • 101
  • 108