深入分析Ruby 變量
變量是持有可被任何程序使用的任何數(shù)據(jù)的存儲(chǔ)位置。
Ruby 支持五種類型的變量。
- 一般小寫字母、下劃線開頭:變量(Variable)。
- $開頭:全局變量(Global variable)。
- @開頭:實(shí)例變量(Instance variable)。
- @@開頭:類變量(Class variable)類變量被共享在整個(gè)繼承鏈中
- 大寫字母開頭:常數(shù)(Constant)。
Ruby 全局變量
全局變量以 $ 開頭。未初始化的全局變量的值為 nil,在使用 -w 選項(xiàng)后,會(huì)產(chǎn)生警告。
給全局變量賦值會(huì)改變?nèi)譅顟B(tài),所以不建議使用全局變量。
下面的實(shí)例顯示了全局變量的用法。
#!/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 是全局變量。這將產(chǎn)生以下結(jié)果:
全局變量在 Class1 中輸出為 10
全局變量在 Class2 中輸出為 10
注意:在 Ruby 中,您可以通過(guò)在變量或常量前面放置 # 字符,來(lái)訪問(wèn)任何變量或常量的值。
Ruby 實(shí)例變量
實(shí)例變量以 @ 開頭。未初始化的實(shí)例變量的值為 nil,在使用 -w 選項(xiàng)后,會(huì)產(chǎn)生警告。
下面的實(shí)例顯示了實(shí)例變量的用法。
#!/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 # 創(chuàng)建對(duì)象 cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # 調(diào)用方法 cust1.display_details() cust2.display_details()
在這里,@cust_id、@cust_name 和 @cust_addr 是實(shí)例變量。這將產(chǎn)生以下結(jié)果:
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala
Ruby 類變量
類變量以 @@ 開頭,且必須初始化后才能在方法定義中使用。
引用一個(gè)未初始化的類變量會(huì)產(chǎn)生錯(cuò)誤。類變量在定義它的類或模塊的子類或子模塊中可共享使用。
在使用 -w 選項(xiàng)后,重載類變量會(huì)產(chǎn)生警告。
下面的實(shí)例顯示了類變量的用法。
#!/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 # 創(chuàng)建對(duì)象 cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # 調(diào)用方法 cust1.total_no_of_customers() cust2.total_no_of_customers()
在這里,@@no_of_customers 是類變量。這將產(chǎn)生以下結(jié)果:
Total number of customers: 1
Total number of customers: 2
Ruby 局部變量
局部變量以小寫字母或下劃線 _ 開頭。局部變量的作用域從 class、module、def 或 do 到相對(duì)應(yīng)的結(jié)尾或者從左大括號(hào)到右大括號(hào) {}。
當(dāng)調(diào)用一個(gè)未初始化的局部變量時(shí),它被解釋為調(diào)用一個(gè)不帶參數(shù)的方法。
對(duì)未初始化的局部變量賦值也可以當(dāng)作是變量聲明。變量會(huì)一直存在,直到當(dāng)前域結(jié)束為止。局部變量的生命周期在 Ruby 解析程序時(shí)確定。
在上面的實(shí)例中,局部變量是 id、name 和 addr。
Ruby 常量
常量以大寫字母開頭。定義在類或模塊內(nèi)的常量可以從類或模塊的內(nèi)部訪問(wèn),定義在類或模塊外的常量可以被全局訪問(wèn)。
常量不能定義在方法內(nèi)。引用一個(gè)未初始化的常量會(huì)產(chǎn)生錯(cuò)誤。對(duì)已經(jīng)初始化的常量賦值會(huì)產(chǎn)生警告。
#!/usr/bin/ruby # -*- coding: UTF-8 -*- class Example VAR1 = 100 VAR2 = 200 def show puts "第一個(gè)常量的值為 #{VAR1}" puts "第二個(gè)常量的值為 #{VAR2}" end end # 創(chuàng)建對(duì)象 object=Example.new() object.show
在這里,VAR1 和 VAR2 是常量。這將產(chǎn)生以下結(jié)果:
第一個(gè)常量的值為 100
第二個(gè)常量的值為 200
Ruby 偽變量
它們是特殊的變量,有著局部變量的外觀,但行為卻像常量。您不能給這些變量賦任何值。
- self: 當(dāng)前方法的接收器對(duì)象。
- true: 代表 true 的值。
- false: 代表 false 的值。
- nil: 代表 undefined 的值。
- __FILE__: 當(dāng)前源文件的名稱。
- __LINE__: 當(dāng)前行在源文件中的編號(hào)。
以上就是深入分析Ruby 變量的詳細(xì)內(nèi)容,更多關(guān)于Ruby 變量的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。