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?
The include method takes all the methods from another module and includes them into the current module. This is a language-level thing as opposed to a file-level thing as with require. The include method is the primary way to "extend" classes with other modules (usually referred to as mix-ins).
The purpose of the . call method is to invoke/execute a Proc/Method instance.
When Ruby executes the def keyword, it simply redefines it (whether the method already exists or not). This is called overriding. See the following example : def language puts("We are learning PHP") end def language puts("We are learning Ruby") end # Now call the method language.
Method names should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence can parse the call incorrectly. Methods should be defined before calling them, otherwise Ruby will raise an exception for undefined method invoking.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With