详细解析Ruby中的变量

这篇文章主要介绍了详细解析Ruby中的变量,是Ruby学习中最基础的知识之一,需要的朋友可以参考下

 变量持有要使用的程序的数据的存储位置。

Ruby支持的有五种类型的变量。在前面的章节中已经经历了一个简短描述以及这些变量。本章中介绍的这五种类型的变量。
Ruby的全局变量:

全局变量以$开头。未初始化的全局变量的值是零,并使用-w选项产生警告。

全局变量的赋值会改变全局状态。这是不推荐使用全局变量。他们使得程序的含义模糊。

下面是一个例子显示使用全局变量。

 #!/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选项。

下面是一个例子显示使用实例变量。

 #!/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 都是实例变量。这将产生以下结果:

 Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, Khandala 

Ruby的类变量:

类变量以@@开始,它们可以被用来在方法定义之前必须初始化。

引用未初始化的类变量产生错误。类变量之间共享其中的类变量定义的类或模块的的后代。

覆盖类变量产生警告-w选项。

下面是一个例子显示使用类变量:

 #!/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 是一类变量。这将产生以下结果:

 Total number of customers: 1 Total number of customers: 2 

Ruby的局部变量:

局部变量以小写字母或_开头。一个局部变量的范围的范围类,模块,def或做相应的结束或块的左花括号的紧密括号{}。

当一个未初始化的局部变量被引用,它被解释为没有参数的方法调用。

分配未初始化的局部变量也作为变量声明。变量开始存在,直到结束的当前范围内到达。局部变量的生命周期由Ruby进行解析程序时才能确定。

另外,在上述的例子中,局部变量 id, name 和他addr.
Ruby的常量:

常量以大写字母开头。在类或模块定义的常量可以在该类或模块访问,所定义外一个类或模块可以全局访问。

常量不能定义在方法内。引用未初始化的常数会产生一个错误。分配已初始化一个常数会产生一个警告。

 #!/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是常量。这将产生以下结果:

 Value of first Constant is 100 Value of second Constant is 200 

Ruby的拟变量:

他们是特殊的变量,局部变量,但外观像常数。但不能给这些变量分配到任何值。

  •     self: 当前方法的接收方对象。
  •     true: 表示真的值。
  •     false: 表示假的值。
  •     nil: 表示未定义(undefined)的值.
  •     __FILE__: 在当前源文件的名称.
  •     __LINE__: 在源文件中的当前行号。

Ruby的基本常值:

Ruby使用字面值的规则是简单和直观。本节介绍了所有基本的Ruby的常值。
整型数:

Ruby支持整数。一个整数的范围可以从 -230 到 230-1 或 -262 to 262-1 在此范围内的整数是Fixnum类的对象,在此范围之外的整数存储在Bignum的类的对象。

编写整数使用可选的前导符号,一个可选的基数表示(0八进制,0x表示十六进制或二进制0b),其次是一串数字在相应基数。下划线字符被忽略的数字串。

例如:

 123         # Fixnum decimal 1_234        # Fixnum decimal with underline -500         # Negative Fixnum 0377         # octal 0xff         # hexadecimal 0b1011        # binary ?a          # character code for 'a' ?\n         # code for a newline (0x0a) 12345678901234567890 # Bignum 

注:类和对象解释在本教程中另一个章节。
浮点数:

Ruby支持整数。他们是数字但带小数。浮点数是Float类的对象,可以是以下任何一种:

例如:

 123.4        # floating point value 1.0e6        # scientific notation 4E20         # dot not required 4e+20        # sign before exponential 

字串常值:

Ruby字符串是简单的8位字节序列,它们是String类的对象。双引号字符串可以替代和反斜线符号,但不允许单引号替换和只允许反斜线符号 \\ 和 \'

例如:

 #!/usr/bin/ruby -w puts 'escape using "\\"'; puts 'That\'s right'; 

这将产生以下结果:

 escape using "\" That's right 

也可以替换成一个字符串使用#{expr}序列表示任何Ruby表达式的值。表达式expr 可以是任何Ruby的表达式。

 #!/usr/bin/ruby -w puts "Multiplication Value : #{24*60*60}"; 

这将产生以下结果:

 Multiplication Value : 86400 

反斜线符号说明:

以下是Ruby支持的反斜线符号列表:

201551290906687.jpg-600 (574×467)

 Ruby字符串的更多详细信息,请通过 Ruby字符串.
Ruby数组:

Ruby的数组是由放置对象引用方括号之间用逗号分隔的一系列字面。逗号结尾被忽略。
例如:

 #!/usr/bin/ruby ary = [ "fred", 10, 3.14, "This is a string", "last element", ] ary.each do |i| puts i end 

这将产生以下结果:

 fred 10 3.14 This is a string last element 

Ruby的数组的更多细节,经过 Ruby数组.
Ruby 哈希:

字面上Ruby创建哈希放置括号之间的键/值对列表,以逗号或序列=>之间的键和值。逗号结尾被忽略。
例如:

 #!/usr/bin/ruby hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f } hsh.each do |key, value| print key, " is ", value, "\n" end 

这将产生以下结果:

 green is 240 red is 3840 blue is 15 

对于更详细的Ruby哈希,经过 Ruby哈希.
Ruby的范围:

范围代表的间隔。一组的开始和结束的值。可能被使用s..e 和s...e文字,或具有Range.new范围。

范围使用..包含运行从开始到结束。创建使用...排除最终值。当作为一个迭代器,范围序列中的每个值将返回。

range (1..5) 表示,它包括1,2,3,4,5值,range (1...5) 表示,它包括1,2,3,4这四个值。
实例:

 #!/usr/bin/ruby (10..15).each do |n| print n, ' ' end 

这将产生以下结果:

 10 11 12 13 14 15 

以上就是详细解析Ruby中的变量的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 脚本专栏