当前位置: 移动技术网 > IT编程>脚本编程>Ruby > 详细解析Ruby中的变量

详细解析Ruby中的变量

2017年12月08日  | 移动技术网IT编程  | 我要评论
 变量持有要使用的程序的数据的存储位置。 ruby支持的有五种类型的变量。在前面的章节中已经经历了一个简短描述以及这些变量。本章中介绍的这五种类型的变量。 r

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

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 (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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网