Self Context
Ruby's self is similar to Java's this, but is quite different. Java methods are referenced in the instance method, so this generally points to the current object. Ruby code is executed row by row, so the context self has different meanings. Let's first look at what common context self represents.
# This position is in top level context, which indicates the default object main
P self # => main
P self. class # => Object
@ Self1 = self
# Because all custom classes are in the main context, this is the object's instance method.
# It can also be said to be a global Method
Def a_method
@ Self2 = self
P self
#=> Main, because the instance variables @ self1 and @ self2 are available, the printed characters are not the main character.
#=> But it is still the main object. Comment out 4 or 8 lines to see the effect.
P @ self1 ==@ self2 # => true
End
# The following is a self about different contexts in the class.
Class person
P self # => person, representing the current class
Def instance_method
P self #=># <Person: 0xb7818fdc> indicates the instance of the current class.
End
Def self. class_method
P self # => Person, which represents the current class (this is the context of the class method) Like row 16th, and they are equal
End
End
M = Person. new
Def m. hello
P self # => stands for m singleton object
End
M. hello
The above only writes self in the class, but also in the module. Through the code above, you can find that self always references the instance/class in the context of its location.
Self explicit/implicit
You can first try to run the following code to see if there is any accident
Class Person
Attr_accessor: name
Def set_name (your_name)
Name = your_name
End
End
M = Person. new
P m. name
M. set_name ('today ')
P m. name # => guess what it is
If you guess it's today, it's a big mistake. The answer is nil. Why is it nil? In line 5th, I obviously called the name = method value generated by attr_accessor, you can try to add self in front, and the code is executed as expected. In this case, name = your_name does not call the xx = method generated by attr_accessor. Instead, name is treated as a local variable. If self is explicitly specified, no problem occurs.
Reading this, do you think it will always be better to use explicit self to call this situation in the future? In fact, the following code will still explain some problems
Class Person
Public
Def get_my_secret1
My_secret # => implicit
End
Def get_my_secret2
Self. my_secret # => explicit
End
Private
Def my_secret
P 'something ...'
End
Def self. secret
P 'nothing'
End
End
M = Person. new
# M. my_secret # => private method error
Person. secret # => nothing
M. get_my_secret1 # => something
M. get_my_secret2 # => private method error
The above Code Description:
The first problem is that explicit self cannot call the private (protected is the same) method, while implicit can, which means implicit is more flexible, however, since the above implicit call also has a certain "misunderstanding", readers should understand the differences between them for better use.
The second problem is that the self method is not restricted by private. In fact, I intentionally misled the reader here, because the self method is a class method, and the permission modifier only takes effect for the instance method, therefore, private class methods can also be accessed directly.
Self "weird" Writing Method
The following code is called a weird way of writing, because it is not commonly used, but can be seen occasionally, but it does not seem intuitive.
Class Person
Def metaclass
Class <self
Self
End
End
Def metaclass2
Self
End
End
A = Person. new
B = a. metaclass
C = a. metaclass2
# First, you must understand that Class Person is an "instance" of Class and Class a is an instance of Person.
# Here, B is also a Person class, but it is unique, that is, you modify the Person will not affect B, and vice versa.
P B #=># <Class: # <Person: 0xb76f3800>
P B. class # => Class
Class Person
Def hello
P'hello Person'
End
End
Class <B
Def hello
P' hello B'
End
End
B. hello # => hello B
P c #=># <Person: 0xb76f3800>
P c. class # => Person
C. hello # => hello Person
There is another
Class Person
Def self. hello
P'hello'
End
Class <self
# After reading the relationship between self and context, you should know that self represents the Person class.
# Here, we will add a class method for Person, which is similar to self. hello.
Def work
P 'hard work'
End
End
End
Person. work
From: http://ilstar.blogbus.com/logs/59782933.html