Brief explanation of the scope of Ruby Object-Oriented Programming and ruby Object-Oriented Programming
Scope
Ruby does not have nested scopes (internal scopes, which can be viewed as external scopes). Their scopes are completely separated. Once a new scope is entered, the original binding will be replaced with a new binding group.
The program will close the previous scope in three places and open a new scope. They are:
- Class Definition class
- Module definition module
- Method definition def
The above three keywords, each keyword corresponds to a scope gate (entry), and the corresponding end corresponds to leaving this door.
Flat Scope
When you enter another scope from one scope, the local variables will immediately become invalid. To make the local variables continuously valid, you can use method calls to replace the scope door by avoiding keywords, let one scope see the variables in another scope to achieve the goal. Specifically, use Class. new to replace class, Module # define_method to replace def, and Module. new to replace module. This is called a flat scope, which means that two scopes are squashed together.
Sample Code (Wrong)
My_var = "Success" class MyClass puts my_var # "Success" def my_method puts my_var cannot be correctly printed here "Success" endend
Sample Code (Right)
my_var = “Success”MyClass = Class.new do puts “#{my_var} in the class definition” define_method :my_method do “#{my_var} in the method” endend
In some languages, such as java or C #, there is an inner scope concept. In the internal scope, you can see the variables in the external scope. But ruby does not have this concept of nested scope. Its scope is completely separated. once it enters a new scope, the original binding will be replaced by a new group of bindings.
In ruby, the program closes the previous scope in three places and opens a new scope: class definition, module definition, and method.
As long as the program enters the class, module, or method definition, the scope switch will take place. The three boundaries use the class, module, and def keywords as the signs respectively. Each keyword acts as a scope gate ).
How can we make the binding pass through a scope door? For example, the following code:
My_var = "hello" class MyClass # here you want to print my_var def my_method #... And here endend
When entering another scope, the local variable will immediately become invalid. If you replace the class keyword with something that does not have a scope gate, such as a method, you can get the value of my_var in a closure and pass the closure to the method. The Code is as follows:
My_var = "hello" MyClass = Class. new do puts "# {my_var} in the class definition" def my_method #... how can I print it out here? Endend
Use the Module # define_method () method to replace def. The Code is as follows:
my_var = “hello”MyClass = Class.new do puts “#{my_var} in the class definition” define_method :my_method do puts “#{my_var} in the method” endend MyClass.new.my_method
hello in the class definitionhello in the method
The method used to replace the scope GATE allows one scope to see the variables in another scope. This technology can be called "flat scope ".
Shared Scope
Define a group of methods. In the flat scope of a variable, you can ensure that the variable is shared by only a limited number of methods. This method is called shared scope.