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?

2

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.