Self in ruby

Source: Internet
Author: User

SELF: indicates the current object or default object in ruby. At any time of program execution, there is only one self.

 

1. Who is self and where is self?

To know which object is self, you must know the current context. The context mainly includes the top-level context, class definition context, module definition context, and method definition context. You can judge the self according to the summary.

Context Example Which object is self?
Top layer Code other than any defined block Main (built-in default top-level object)
Class Definition Class C Class Object C
Module Definition Module M Module object m
Method Definition

1 top layer method

Def method_name

 

2. instance method definition

Class C

Def method_name

 

3. instance method definition in the module

Module M

Def method_name

 

4 Singleton methods (including class methods)

Def obj. method_name

Main (built-in default top-level object)

 

 

A c instance object. This instance object responds to the method_name method call.

 

 

 

(1) A single object extended by m

(2) An object instance mixed with M classes

 

 

OBJ

 

1.1 self in the top layer

The self in the top-level context is main, and the self in the top-level method is also main. Main is an object of an object.

 

1.2 class and self defined in the module

Self in the class is the class object itself

The object of the instance method in the class is the instance object.

The module self is the module name.

 

1.3 self in the singleton Method

As an object, self in its Singleton method is obj.

 

2. self is the default receiver of the message.

Method call is generally in the obj. Method method. It is marked with a dot, the receiver on the left, and the method on the right. However, when the receiver is self, the receiver and the dot can be omitted. Ruby uses self as the default receiver, which means that the messages you send will be sent to self. That is, method is equivalent to self. method.

The class method calls hello. It is equivalent to self. Hello. In the class, "Hello world" is printed, which is equivalent to a. Hello. When hello is used outside the class, self is main, and this function does not exist, so an error is returned. Of course a. Hello is correct.

However, it is worth noting that when calling the write method (the method ended with an equal sign), even if the message is sent to the current self, it cannot be omitted. For example, to call method venue =, write self. Venue = "hello ". If it is written as venue = "hello", Ruby interprets it as a value assignment to a local variable.

 

3. instance variables and self

In Ruby, the instance variable starts. It is worth noting that any instance variables seen in the ruby program belong to the current object self at this position in the program.

The first @ var belongs to Class A. Therefore, it is executed after the class is created and hello is printed. @ VaR in the method belongs to the Instance Object of the class. Therefore, after creating the Instance Object of A, call the method to print the world. The two @ VaR are completely irrelevant. Any object can have instance variables-its information and private storage of the object state.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.