这篇文章主要介绍了详细解析Ruby中的变量,是Ruby学习中最基础的知识之一,需要的朋友可以参考下
变量持有要使用的程序的数据的存储位置。
Ruby支持的有五种类型的变量。在前面的章节中已经经历了一个简短描述以及这些变量。本章中介绍的这五种类型的变量。
Ruby的全局变量:
全局变量以$开头。未初始化的全局变量的值是零,并使用-w选项产生警告。
全局变量的赋值会改变全局状态。这是不推荐使用全局变量。他们使得程序的含义模糊。
下面是一个例子显示使用全局变量。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!/usr/bin/ruby $global_variable = 10 class Class1 def print_global puts "Global variable in Class1 is #$global_variable" end end class Class2 def print_global puts "Global variable in Class2 is #$global_variable" end end class1obj = Class1.new class1obj.print_global class2obj = Class2.new class2obj.print_global这里$global_variable是一个全局变量。这将产生以下结果:
注意: 在Ruby中,把一个哈希号(#)字符,在任意变量或常量之前能够访问它的值。
Global variable in Class1 is 10
Global variable in Class2 is 10
Ruby的实例变量:
实例变量@开始。未初始化的实例变量的值是零,并产生警告-w选项。
下面是一个例子显示使用实例变量。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #!/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 Objects cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # Call Methods cust1.display_details() cust2.display_details()这里的@cust_id, @cust_name 和 @cust_addr 都是实例变量。这将产生以下结果:
?
1 2 3 4 5 6 Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, KhandalaRuby的类变量:
类变量以@@开始,它们可以被用来在方法定义之前必须初始化。
引用未初始化的类变量产生错误。类变量之间共享其中的类变量定义的类或模块的的后代。
覆盖类变量产生警告-w选项。
下面是一个例子显示使用类变量:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!/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 # Create Objects cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # Call Methods cust1.total_no_of_customers() cust2.total_no_of_customers()@@no_of_customers 是一类变量。这将产生以下结果:
?
1 2 Total number of customers: 1 Total number of customers: 2Ruby的局部变量:
局部变量以小写字母或_开头。一个局部变量的范围的范围类,模块,def或做相应的结束或块的左花括号的紧密括号{}。
当一个未初始化的局部变量被引用,它被解释为没有参数的方法调用。
分配未初始化的局部变量也作为变量声明。变量开始存在,直到结束的当前范围内到达。局部变量的生命周期由Ruby进行解析程序时才能确定。
另外,在上述的例子中,局部变量 id, name 和他addr.
Ruby的常量:
常量以大写字母开头。在类或模块定义的常量可以在该类或模块访问,所定义外一个类或模块可以全局访问。
常量不能定义在方法内。引用未初始化的常数会产生一个错误。分配已初始化一个常数会产生一个警告。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/usr/bin/ruby class Example VAR1 = 100 VAR2 = 200 def show puts "Value of first Constant is #{VAR1}" puts "Value of second Constant is #{VAR2}" end end # Create Objects object=Example.new() object.show这里VAR1和VAR2是常量。这将产生以下结果:
?
1 2 Value of first Constant is 100 Value of second Constant is 200Ruby的拟变量:
他们是特殊的变量,局部变量,但外观像常数。但不能给这些变量分配到任何值。
self: 当前方法的接收方对象。
true: 表示真的值。
false: 表示假的值。
nil: 表示未定义(undefined)的值.
__FILE__: 在当前源文件的名称.