變量是持有可被任何程序使用的任何數據的存儲位置。
Ruby 支持五種類型的變量。
- 一般小寫字母、下劃線開頭:變量(Variable)。
- $開頭:全局變量(Global variable)。
- @開頭:實例變量(Instance variable)。
- @@開頭:類變量(Class variable)類變量被共享在整個繼承鏈中
- 大寫字母開頭:常數(Constant)。
Ruby 全局變量
全局變量以 $ 開頭。未初始化的全局變量的值為 nil,在使用 -w 選項后,會產生警告。
給全局變量賦值會改變全局狀態,所以不建議使用全局變量。
下面的實例顯示了全局變量的用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#!/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 選項后,會產生警告。
下面的實例顯示了實例變量的用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/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 選項后,重載類變量會產生警告。
下面的實例顯示了類變量的用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#!/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 常量
常量以大寫字母開頭。定義在類或模塊內的常量可以從類或模塊的內部訪問,定義在類或模塊外的常量可以被全局訪問。
常量不能定義在方法內。引用一個未初始化的常量會產生錯誤。對已經初始化的常量賦值會產生警告。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/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__: 當前行在源文件中的編號。
以上就是深入分析Ruby 變量的詳細內容,更多關于Ruby 變量的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.runoob.com/ruby/ruby-variable.html