歡迎您光臨本站 註冊首頁

深入分析Ruby 變數

←手機掃碼閱讀     zmcjlove @ 2020-06-29 , reply:0

變數是持有可被任何程式使用的任何資料的儲存位置。

Ruby 支援五種型別的變數。

  • 一般小寫字母、下劃線開頭:變數(Variable)。

  • $開頭:全域性變數(Global variable)。

  • @開頭:例項變數(Instance variable)。

  • @@開頭:類變數(Class variable)類變數被共享在整個繼承鏈中

  • 大寫字母開頭:常數(Constant)。

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 是全域性變數。這將產生以下結果:

全域性變數在 Class1 中輸出為 10
 全域性變數在 Class2 中輸出為 10

注意:在 Ruby 中,您可以透過在變數或常量前面放置 # 字元,來訪問任何變數或常量的值。

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 偽變數

它們是特殊的變數,有著區域性變數的外觀,但行為卻像常量。您不能給這些變數賦任何值。

  • self: 當前方法的接收器物件。

  • true: 代表 true 的值。

  • false: 代表 false 的值。

  • nil: 代表 undefined 的值。

  • __FILE__: 當前原始檔的名稱。

  • __LINE__: 當前行在原始檔中的編號。



[zmcjlove ] 深入分析Ruby 變數已經有430次圍觀

http://coctec.com/docs/program/show-post-240285.html