良许Linux教程网 干货合集 详解Ruby的五种变量

详解Ruby的五种变量

Ruby支持的有五种类型的变量,分别是全局变量、实例变量、类变量、局部变量、常量、伪变量,下面为大家详细的讲解一下Ruby的五种变量。

详解Ruby的五种变量

Ruby 全局变量

全局变量以 $ 开头。未初始化的全局变量的值为 nil,在使用 -w 选项后,会产生警告。 给全局变量赋值会改变全局状态,所以不建议使用全局变量。 下面的实例显示了全局变量的用法。 实例

#!/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

在这里,$global_variable 是全局变量。这将产生以下结果: 注意:在 Ruby 中,您可以通过在变量或常量前面放置 # 字符,来访问任何变量或常量的值。

全局变量在 Class1 中输出为 10
全局变量在 Class2 中输出为 10

Ruby 实例变量

实例变量以 @ 开头。未初始化的实例变量的值为 nil,在使用 -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

# 创建对象
cust1=Customer.new("1""John""Wisdom Apartments, Ludhiya")
cust2=Customer.new("2""Poul""New Empire road, Khandala")

# 调用方法
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

# 创建对象
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()

在这里,@@no_of_customers 是类变量。这将产生以下结果:

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

Ruby 局部变量

局部变量以小写字母或下划线 _ 开头。局部变量的作用域从 class、module、def 或 do 到相对应的结尾或者从左大括号到右大括号 {}。 当调用一个未初始化的局部变量时,它被解释为调用一个不带参数的方法。 对未初始化的局部变量赋值也可以当作是变量声明。变量会一直存在,直到当前域结束为止。局部变量的生命周期在 Ruby 解析程序时确定。 在上面的实例中,局部变量是 id、name 和 addr。

Ruby 常量

常量以大写字母开头。定义在类或模块内的常量可以从类或模块的内部访问,定义在类或模块外的常量可以被全局访问。 常量不能定义在方法内。引用一个未初始化的常量会产生错误。对已经初始化的常量赋值会产生警告。 实例

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

class Example
  VAR1 = 100
  VAR2 = 200
  def show
      puts "第一个常量的值为 #{VAR1}"
      puts "第二个常量的值为 #{VAR2}"
  end
end

# 创建对象
object=Example.new()
object.show

在这里,VAR1 和 VAR2 是常量。这将产生以下结果:

第一个常量的值为 100
第二个常量的值为 200

Ruby 伪变量

它们是特殊的变量,有着局部变量的外观,但行为却像常量。您不能给这些变量赋任何值。

  • 1.self: 当前方法的接收器对象。
  • 2.true: 代表 true 的值。
  • 3.false: 代表 false 的值。
  • 4.nil: 代表 undefined 的值。
  • 5.FILE: 当前源文件的名称。
  • 6.LINE: 当前行在源文件中的编号。

以上就是良许教程网为各位朋友分享的Linux系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部