詳細解析Ruby中的變數
這篇文章主要介紹了詳細解析Ruby中的變數,是Ruby學習中最基礎的知識之一,需要的朋友可以參考下
變數持有要使用的程式的資料的儲存位置。
Ruby支援的有五種類型的變數。在前面的章節中已經經曆了一個簡短描述以及這些變數。本章中介紹的這五種類型的變數。
Ruby的全域變數:
全域變數以$開頭。未初始化的全域變數的值是零,並使用-w選項產生警告。
全域變數的賦值會改變全域狀態。這是不推薦使用全域變數。他們使得程式的含義模糊。
下面是一個例子顯示使用全域變數。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/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選項。
下面是一個例子顯示使用執行個體變數。
?
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 # 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 都是執行個體變數。這將產生以下結果:
?
1 2 3 4 5 6 |
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 # 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 是一類變數。這將產生以下結果:
?
1 2 |
Total number of customers: 1 Total number of customers: 2 |
Ruby的局部變數:
局部變數以小寫字母或_開頭。一個局部變數的範圍的範圍類,模組,def或做相應的結束或塊的左花括弧的緊密括弧{}。
當一個未初始化的局部變數被引用,它被解釋為沒有參數的方法調用。
分配未初始化的局部變數也作為變數聲明。變數開始存在,直到結束的當前範圍內到達。局部變數的生命週期由Ruby進行解析程式時才能確定。
另外,在上述的例子中,局部變數 id, name 和他addr.
Ruby的常量:
常量以大寫字母開頭。在類或模組定義的常量可以在該類或模組訪問,所定義外一個類或模組可以全域訪問。
常量不能定義在方法內。引用未初始化的常數會產生一個錯誤。分配已初始化一個常數會產生一個警告。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/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是常量。這將產生以下結果:
?
1 2 |
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),其次是一串數字在相應基數。底線被忽略的數字串。
例如:
?
1 2 3 4 5 6 7 8 9 |
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類的對象,可以是以下任何一種:
例如:
?
1 2 3 4 |
123.4 # floating point value 1.0e6 # scientific notation 4E20 # dot not required 4e+20 # sign before exponential |
字串常值:
Ruby字串是簡單的8位位元組序列,它們是String類的對象。雙引號字串可以替代和反斜線符號,但不允許單引號替換和只允許反斜線符號 和 '
例如:
?
1 2 3 4 |
#!/usr/bin/ruby -w puts 'escape using ""'; puts 'That's right'; |
這將產生以下結果:
?
1 2 |
escape using "" That's right |
也可以替換成一個字串使用#{expr}序列表示任何Ruby運算式的值。運算式expr 可以是任何Ruby的運算式。
?
1 2 3 |
#!/usr/bin/ruby -w puts "Multiplication Value : #{24*60*60}"; |
這將產生以下結果:
?
1 |
Multiplication Value : 86400 |
反斜線符號說明:
以下是Ruby支援的反斜線符號列表:
Ruby字串的更多詳細資料,請通過 Ruby字串.
Ruby數組:
Ruby的數組是由放置對象引用方括弧之間用逗號分隔的一系列字面。逗號結尾被忽略。
例如:
?
1 2 3 4 5 6 |
#!/usr/bin/ruby ary = [ "fred", 10, 3.14, "This is a string", "last element", ] ary.each do |i| puts i end |
這將產生以下結果:
?
1 2 3 4 5 |
fred 10 3.14 This is a string last element |
Ruby的數組的更多細節,經過 Ruby數組.
Ruby 雜湊:
字面上Ruby建立雜湊放置括弧之間的鍵/值對列表,以逗號或序列=>之間的鍵和值。逗號結尾被忽略。
例如:
?
1 2 3 4 5 6 |
#!/usr/bin/ruby hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f } hsh.each do |key, value| print key, " is ", value, "n" end |
這將產生以下結果:
?
1 2 3 |
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這四個值。
執行個體:
?
1 2 3 4 5 |
#!/usr/bin/ruby (10..15).each do |n| print n, ' ' end |
這將產生以下結果:
?