Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does def `self.function` name mean?

Tags:

ruby

Can anyone explain to me what the meaning of adding self to the method definition is? Is it similar to the this keyword in java?

like image 526
Dude Avatar asked Dec 04 '12 15:12

Dude


People also ask

What does def self mean?

When declaring a method, the self of the declaration is the declaring class/module, so effectively you are defining a class method. For the client, this works similar to a static method in java. The client would call the method on the class instead of an instance: MyClass.method.

What is Def self Ruby?

The keyword self in Ruby enables you to access to the current object — the object that is receiving the current message. The word self can be used in the definition of a class method to tell Ruby that the method is for the self, which is in this case the class.

What does self refer to in an instance method body?

the method self refers to the object it belongs to. Class definitions are objects too. If you use self inside class definition it refers to the object of class definition (to the class) if you call it inside class method it refers to the class again.

How do you define a class method in Ruby?

Class Methods are the methods that are defined inside the class, public class methods can be accessed with the help of objects. The method is marked as private by default, when a method is defined outside of the class definition. By default, methods are marked as public which is defined in the class definition.


1 Answers

Contrary to other languages, Ruby has no class methods, but it has singleton methods attached to a particular object.

cat = String.new("cat") def cat.speak     'miaow' end cat.speak #=> "miaow"  cat.singleton_methods #=> ["speak"]  

def cat.speak creates a singleton method attached to the object cat.

When you write class A, it is equivalent to A = Class.new :

A = Class.new def A.speak     "I'm class A" end A.speak #=> "I'm class A"  A.singleton_methods #=> ["speak"]  

def A.speak creates a singleton method attached to the object A. We call it a class method of class A.

When you write

class A     def self.c_method         'in A#c_method'     end end 

you create an instance of Class(*). Inside the class definition, Ruby sets self to this new instance of Class, which has been assigned to the constant A. Thus def self.c_method is equivalent to def cat.speak, that is to say you define a singleton method attached to the object self, which is currently the class A.

Now the class A has two singleton methods, that we commonly call class methods.

A.singleton_methods  => ["c_method", "speak"]  

(*) technically, in this case where A has already been created by A = Class.new, class A reopens the existing class. That's why we have two singleton methods at the end. But in the usual case where it is the first definition of a class, it means Class.new.

like image 96
BernardK Avatar answered Sep 20 '22 13:09

BernardK