While looking over some Ruby code I noticed methods declared with self. prepended to the method name. For example:
def self.someMethod
//...
end
What does prepending self. to the method name change about the method?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
def self.somethingis a class method, called with:def somethingis an instance method, called with:The difference is that one is called on the class itself, the other on an instance of the class.
To define a class method, you can also use the class name, however that will make things more difficult to refactor in the future as the class name may change.
Some sample code: