HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Variable is holding any data can be used by any program stored.
Ruby supports five types of variables.
$
At the beginning: a global variable (Global variable). @
The beginning: the instance variables (Instance variable). @@
Beginning: Class variable (Class variable) Class variables are shared across the inheritance chain You have in previous chapters about understanding these variables, this chapter will provide you a detailed explanation of these five types of variables.
Global variables start with $. Uninitialized global variable isnil,in the -w option, a warning is generated.
To the global variable assignment will change global state, so we do not recommend the use of global variables.
The following example shows the use of global variables.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- $global_variable = 10 class Class1 def print_global puts "全局变量在 Class1 中输出为 #$global_variable" end end class Class2 def print_global puts "全局变量在 Class2 中输出为 #$global_variable" end end class1obj = Class1.new class1obj.print_global class2obj = Class2.new class2obj.print_global
Here, $ global_variable is a global variable. This produces the following results:
Note: In Ruby, you can place the # character in front of a variable or constant, to access any variable or constant value.
全局变量在 Class1 中输出为 10 全局变量在 Class2 中输出为 10
@ Instance variable to begin with. Uninitialized instance variable isnil,in the -w option, a warning is generated.
The following example shows the use of instance variables.
#! / Usr / bin / ruby class Customer def initialize (id, name, addr) @ Cust_id = id @ Cust_name = name @ Cust_addr = addr end def display_details () puts "Customer id # @ cust_id" puts "Customer name # @ cust_name" puts "Customer address # @ cust_addr" end end # Create Object cust1 = Customer.new ( "1", "John", "Wisdom Apartments, Ludhiya") cust2 = Customer.new ( "2", "Poul", "New Empire road, Khandala") # Call method cust1.display_details () cust2.display_details ()
Here, @ cust_id, @ cust_name and @cust_addr is an instance variable. This produces the following results:
Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, Khandala
In the beginning of @@ class variables and must be initialized before you can use later in the method definition.
Reference an uninitialized variable class will generate an error. Class variables defined in the class or subclass of modules or sub-modules can be shared.
After using the -w option, overloading class variables will produce a warning.
The following example shows the use of class variables.
#!/usr/bin/ruby class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" end end # 创建对象 cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # 调用方法 cust1.total_no_of_customers() cust2.total_no_of_customers()
Here, @@ no_of_customers is a class variable. This produces the following results:
Total number of customers: 1 Total number of customers: 2
Local variables begin with a lowercase letter or an underscore _. The scope of local variables from class, module, def or do the corresponding end of the brace to the left or right curly braces {}.
When you call an uninitialized local variable, it is interpreted as calling a method with no parameters.
Uninitialized local variable assignment can also be used as a variable declaration. Variable will exist until the end of the current domain so far. Lifecycle local variables determined at Ruby parser.
In the example above, the local variable is the id, name and addr.
Constant with an uppercase letter. Define constants within the class or module can be accessed from within the class or module, defined outside a class or module constants can be accessed globally.
Constants can not be defined within the method. Reference an uninitialized constant error occurs. Already initialized constant values will generate a warning.
#! / Usr / bin / ruby # - * - Coding: UTF-8 - * - class Example VAR1 = 100 VAR2 = 200 def show puts "The first constant is # {VAR1}" puts "The second constant is # {VAR2}" end end # Create Object object = Example.new () object.show
Here, VAR1 and VAR2 are constants. This produces the following results:
The first constant value of 100 The second constant is 200
They are special variables, with the appearance of local variables, but acts like a constant. You can not assign any value to these variables.