A brief interpretation of the scope of ruby object-oriented programming

Time:2021-12-23

Scope

RubyIt does not have the characteristics of nested scopes (that is, in the internal scope, you can see the external scope). Its scopes are completely separated. Once you enter a new scope, the original binding will be replaced by a new set of bindings.

The program will close the previous scope in three places and open a new scope at the same time. They are:

  • Class definition
  • Module definition module
  • Method definition def

For the above three keywords, each keyword corresponds to a scope gate (entry), and the corresponding end corresponds to leaving this gate.

Flatten scope

When you enter another scope from one scope, the local variables will become invalid immediately. In order to keep the local variables valid, you can use method calls to replace the scope gate by avoiding keywords, so that one scope can see the variables in another scope, so as to achieve the purpose. Specifically, through class New replaces class, module #define_ Method instead of def, module New replaces module. This is called a flat scope, which means that the two scopes are squeezed together.

Example code (wrong)

my_ var = “Success”

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”
  end
end

In some languages, such as Java or C #, there is the concept of internal scope. In the internal scope, you can see the variables in the external scope. However, ruby does not have the concept of nested scope. Its scope is completely separated. Once you enter a new scope, the original binding will be replaced by a new set of bindings.

In ruby, the program closes the previous scope in three places and opens a new scope: class definition, module definition and method.

Scope switching occurs whenever the program enters the definition of a class, module, or method. These three boundaries are marked by class, module and def keywords respectively, and each keyword acts as a scope gate.

How do I get a binding to cross a scope gate? For example, the following code:

my_ var = “hello”

When entering another scope, the local variable will be invalidated immediately. If you replace the class keyword with something that is not a scope gate, such as a method, you can get my in a closure_ VaR and pass this closure to the method. The code is as follows:

my_ var = “hello”

Using module #define_ The method () method can 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”
     end
end


 
MyClass.new.my_method

hello in the class definition
hello in the method

Using method instead of scope gate can make one scope see variables in another scope. This technology can be called “flat scope”.

Shared scope

Defining a set of methods into the flat scope of a variable can ensure that variables are shared only by a limited number of methods. This method is called shared scope.

Recommended Today

IOS memory partition

#Stack area It is a continuous data structure expanded from high address to low address, which is used to store the values of local variables and function parameters #Heap area Programmers need to manage and release the data structure domain expanded by low address like high address and discontinuous memory area #Global area Global variables […]