The meaning of Ruby self in different environments _ruby topics

Source: Internet
Author: User
And since Ruby is a completely pure object-oriented language, anything is an object, the method is an object, and the class is an object ..., so self has a lot of environments that distinguish the self meaning of different environments to better understand the meaning of the program.
First level
Ruby Code
Puts self
Print out Main, which represents the default object main.
Second, in the definition of class or module:
In the definition of class and module, self represents this class or the module object:
Ruby Code
Class S
Puts ' Just started class S '
Puts self
Module M
Puts ' Nested module s::m '
Puts self
End
Puts ' back in the outer level of S '
Puts self
End
Output results:
Wrote
>ruby self1.rb
Just started class S
Nested Module S::m
S::m
Back at the outer level of S
>exit code:0
Iii. in the method definition of the instance:
This is the same thing as this one in Java: The object that the program automatically passes to call this method
Java code
Class S
def m
Puts ' Class S method M: '
Puts self
End
End
s = s.new
S.m
Run Result:
Wrote
>ruby self2.rb
Class S Method M:
#<s:0x2835908>
>exit code:0
Iv. in a single case method or class method:
A single method is a method that is added to an object, and only this object owns and accesses the method, at which point self is the object that owns the method:
Ruby Code
# SELF3.RB
obj = object.new
def obj.show
print ' I am an object: '
Puts "Here's self inside a singleton method of mine:"
Puts self
End
Obj.show
print ' and inspecting obj from outside, '
Puts "to be sure it ' s the same object:"
Puts obj
Run Result:
Wrote
Ruby Self3.rb
I am a object:here ' s self inside a singleton method of mine:
#<object:0x2835688>
and inspecting obj from outside, to be sure it ' s same object:
#<object:0x2835688>
>exit code:0
Self represents this class object in the class method:
Ruby Code
# SELF4.RB
Class S
def s.x
Puts "class method of Class S"
Puts self
End
End
S.x
Run Result:
Wrote
>ruby self4.rb
class method of Class S
>exit code:0
From the example above we can see that both Ruby Self and Java this represents the current or default object that you can access in the current environment.
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.