Variable Learning in Ruby summary _ruby topics

Source: Internet
Author: User

There are several forms of variables in Ruby, which are local variables, instance variables, class variables, and global variables. For beginners, it is often easy to confuse, especially if I do java like this, I still have a headache to understand, after careful Identify learning, and summarize the differences and usage scenarios of these variables:

First, the naming method

1. Local variables: lowercase letters or "_" at the beginning, such as user, the interpreter only exists in memory when it is interpreted. A good practice is to use nil to initialize the variable when defining variables, otherwise it will appear similar to undefined local variable or method `z 'error;
2. Instance variables: Beginning with @, such as @user, the default initialization is nil, where "instance" refers to the class object itself, or the object of the class;
3. Class variables: @@, such as @@ user, need to be initialized before they can be called, otherwise an error similar to uninitialized class variable @@ user in Account will occur;
4. Global variables: beginning with $, such as $ user, initialized to nil by default;

Second, the scope of action

Local variables: such as user, only affect this class, this method or this module, different from java language conventions, variables defined in a class can be called in class methods, local variables in Ruby, defined in the class , Can only be accessed in the class, and its subclasses, methods, and inner classes cannot be called. The following example code:
Copy the code:

class Account
 user = User.new
 def myMethod
  puts user.name #An error occurred in the call, user is undefined
 end
end
Instance variables: such as @user, a bit similar to the member variables of the POJO class in Java. They are accessed freely in the class and accessed by methods outside the class. They act within the scope of the instance object or the scope of the instance object of the class. , Please pay attention to distinguish between the class object itself [Account, is an object of the Class class] and the object after the class is instantiated [Account.new, an object of the Account class]), these are two mutually independent domains, defined Variables in Account cannot be accessed in Account.new and vice versa, as shown in the following example code:

Copy the code:

class A
  #Class instance variables of the class can be assigned or not assigned before access. If not assigned, it is nil
  @ alpha = 'This is @alpha \' value! '
 
  def A.look
   puts "# @ alpha"
  end
  def look
   puts "# @ alpha"
  end
end
A.look #Output: 'This is @alpha' value! ''
A.new.look #Output: ''
Look at the following code:

Copy the code:

class A
  #Class instance variables of the class can be assigned or not assigned before access. If not assigned, it is nil
  @ alpha = 'This is @alpha \' value! '
  def A.look
   puts "# @ alpha"
  end
  def look
   @ alpha = 'This is @alpha \' value from look! '
   puts "# @ alpha"
  end
  def look_again
   puts "# @ alpha"
  end
end

A.look #Output: 'This is @alpha' value! ''
a = A.new
a.look #Output: 'This is @alpha' value from look! '
a.look_again #Output: 'This is @alpha' value from look! '
It can be seen that @ alpha = 'This is @alpha \' value! 'Is an instance variable defined in the class object itself, @ alpha =' This is @alpha \ 'value from look!' Is defined in the object after the class is instantiated In addition, there are a few points to note when using instance variables:

1. Whether an instance variable is defined in a class or a method, it belongs to a class, not a method
2. Instance variables exist only in the instance scope, and cannot be referenced or assigned in subclasses.
3. Instance variables are always Private and cannot be exposed as Public. External access is performed through methods and can be easily defined using attr_accessor

Class variables: such as @@ user, which acts on all scopes of a class and is shared by all instance objects, including subclasses and their instance objects. Class variables are declared through Protected, as shown in the following example code:
Copy the code:

class A
  #Class instance variables of the class can be assigned or not assigned before access. If not assigned, it is nil
  @@ alpha = 'This is @alpha \' value! '
  def A.look
   puts "# @@ alpha"
  end
  def look
   puts "# @@ alpha"
  end
  def look_again
   puts "# @@ alpha"
  end
end

class B <A
end

A.look #Output: 'This is @alpha' value! '
B.look #Output: 'This is @alpha' value! '
a = A.new
a.look #Output: 'This is @alpha' value! '
a.look_again #Output: 'This is @alpha' value! '
b = B.new
b.look #Output: 'This is @alpha' value! '
b.look_again #Output: 'This is @alpha' value! '
Global variables: such as $ user, use as little as possible, affecting the entire program life cycle, resident memory, excessive use will cause performance degradation, memory overflow, Ruby has built-in global variables, it is very convenient to get related data, such as $ 0 ' Is the file name of the running application, '$:' represents the default file search path; '$$' represents the process id of the ruby program.


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.