Ruby程序快速入門之數據結構
就象許多程序語言一樣,Ruby也提供了完整的數據結構來存儲和管理數據與對象。數組是使用方括號和用逗號隔開的單個對象參考列表創建的。
presidents=["John","Richard","Gerald","Ronald","George","William"]; |
為了更為容易地創建一個充滿單詞的數組,Ruby提供了一個特殊的標志來消除雙引號和逗號,見下面例子:
presidents= %w[ John Richard Gerald Ronald George William]; |
在其它編程語言中,"數組"一詞經常意味著一組相同性質的對象的集合。但在Ruby中,不是這樣。在Ruby中,一個"數組"可以是由不同性質的對象參考組成的集合。因此,下面是有效的數組表達形式:
order_date=Date.today() shirt_information=[14.5,"Long",32,order_date] |
在這個數組中,對象參考按順序存儲并索引。象Java一樣,索引從0開始,索引可用來從數組中檢索對象參考。下面的示例中請求的是,上面創建的shirt_information數組中的第3個元素(索引為2)。注意,你可以使用方括號標志或at方法來檢索數組中的對象參考。
irb(main):003:0> shirt_information[2] => 32 irb(main):004:0> shirt_information.at(2) => 32 |
有趣的是,你還可以使用一個負數索引來引用數組中的元素。一個負數索引是從數組尾部開始計數的。
irb(main):005:0> shirt_information[-3] => "Long" |
數組是動態的,意味著數組的大小可以根據你的操作而動態地改變。你可以使用[index]=操作符來添加或替換一個數組中的元素。
irb(main):013:0> shirt_information => [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>] irb(main):014:0> shirt_information[1]="Medium" #change shirt length => "Medium" irb(main):015:0> shirt_information[4]=49.99 #add shirt cost => 49.99 irb(main):016:0> shirt_information => [14.5, "Medium", 32, #<Date: 4907585/2,0,2299161>, 49.99] |
你也可以使用數字對和范圍來由數組的一部分創建一個新數組,通過使用一個[開始索引,元素數目]標志或[開始索引...結束索引]標志。
irb(main):019:0> shirt_information => [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99] irb(main):020:0> shirt_dimensions = shirt_information[0,3] => [14.5, "Long", 32] irb(main):021:0> shirt_order = shirt_information[2..5] => [32, #<Date: 4907585/2,0,2299161>, 49.99] irb(main):030:0> shirt_information[-3,2] => [32, #<Date: 4907585/2,0,2299161>] |
這個結合了賦值運算符([]=)的標志將產生一個很復雜的元素插入或代替操作。一個開始/結束索引或范圍可以用在賦值操作符中。具體使用請參考下面的示例:
irb(main):001:0> test_array=["zero", "one", "two", "three", "four"] => ["zero", "one", "two", "three", "four"] irb(main):002:0> #starting at the second element, replace the next two elements with a single element irb(main):003:0* test_array[1,2]=1 => 1 irb(main):004:0> test_array => ["zero", 1, "three", "four"] irb(main):005:0> #insert a new element after the second one (zero as a second parameter indicates "insert") irb(main):006:0* test_array[2,0]=2 => 2 irb(main):007:0> test_array => ["zero", 1, 2, "three", "four"] irb(main):008:0> #add an array of elements after element 5 irb(main):009:0* test_array[5,0]=[5,6,7] => [5, 6, 7] irb(main):010:0> test_array => ["zero", 1, 2, "three", "four", 5, 6, 7] irb(main):011:0> #replace elements in the index range of 3..4 with the array assigned irb(main):012:0* test_array[3..4]=[3,4] => [3, 4] irb(main):013:0> test_array => ["zero", 1, 2, 3, 4, 5, 6, 7] |
最后,也許Ruby數組中最有力量的運算可以從數學操作符中找到答案-它們能夠從現在數組創建新的數組。例如,+操作符允許你由兩個數組的連接創建一個新數組,而*操作符允許你復制或連接一個數組自身若干次。
irb(main):033:0> shirt_information => [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99] irb(main):034:0> pant_information=[34,32,59.99,order_date] => [34, 32, 59.99, #<Date: 4907585/2,0,2299161>] irb(main):035:0> shirt_information + pant_information => [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99, 34, 32, 59.99, #<Date: 4907585/2,0,2299161>] irb(main):036:0> shirt_information * 2 => [14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99, 14.5, "Long", 32, #<Date: 4907585/2,0,2299161>, 49.99] irb(main):037:0> array1 = [2,4,6,8,10] => [2, 4, 6, 8, 10] irb(main):038:0> array2=[3,6,9] => [3, 6, 9] irb(main):039:0> array1 - array2 => [2, 4, 8, 10] |
在Ruby中,與數組聯系最密切的就是哈希表。在一個數組中,索引是一個整數。在Ruby中Hash類行為上與Array非常相似,但是它允許在集合中出現到參考對象的任何類型的對象"索引"(或鍵)。在另外的程序語言中,這可能被稱為一個字典或地圖或哈希地圖。一般地,當使用哈希表時,你應該為集合中的每個元素提供兩個對象參考。其中,一個對象參考用作鍵,另一個用作該鍵所指對象(即"值")。用于指示一個鍵對象所指內容的標志是=>符號。當創建哈希表時,鍵/值對可以在兩個大括號之間被收集。作為一個哈希表的例子,你可以在哈希表中使用簡單的字符串對象作為鍵來引用Date對象(值)。
holidays={"New Year"=>Date.parse("2006-01-02"), "MLB Birthday"=> Date.parse("2006-01-16"), "Washington Birthday"=>Date.parse("2006-02-20"), "Memorial Day"=>Date.parse("2006-05-29"), "July 4th"=>Date.parse("2006-07-05")} |
為了檢索紀念日的Date對象,你可以使用"Memorial Day"字符串鍵。
irb(main):004:0> holidays["Memorial Day"] => # irb(main):005:0> holidays["Memorial Day"].to_s => "2006-05-29" |
如你所想,在數組和哈希表中還有另外許多方法來允許你存取和修改集合中的單個元素或改變全部集合本身。因此,在Ruby中,數組和哈希表都是功能極強的動態的數據結構。
一旦你有一個對象參考的集合,那么在編程中最常用的一項任務就是遍歷該集合中的每一個元素并執行某些操作。Ruby當然也提供了數組和哈希表中的類似機制。Ruby提供的實現遍歷集合的機制是該集合對象上的一組方法。Array中的方法(如each,each_index,delete_if)和Hash中的方法(如each,each_key,each_value,each_pair)允許你的編碼進行集合遍歷并執行特定的任務。事實上,Ruby中的許多類都包含迭代子(iterator)方法。例如,String就提供了一個iterator方法來執行字符串的以字符或字節為單位的遍歷任務。然而,在你理解iterator方法前,你首先需要理解Ruby中代碼塊的概念。因為當遍歷集合中的元素時,對于每一個iterator方法,都是通過傳遞一個代碼塊來執行的。