## 2014年10月8日 星期三

### [ Ruby Gossip ] Basic : 內建型態與操作 - 陣列型態

Source From Here
Preface

>> array = [3, 1, 4, 5, 2, 7]
=> [3, 1, 4, 5, 2, 7]
>> array.sort
=> [1, 2, 3, 4, 5, 7]
>> array.sort { |a, b| b <=> a }
=> [7, 5, 4, 3, 2, 1]
>> array
=> [3, 1, 4, 5, 2, 7] # 原本 array 內容不變

sort 方法可以自行指定排序方式，上例中第二個 sort 範例，{} 區塊中會傳入陣列中兩元素給 a、b 參考，你要傳回 0、大於0 或 小於0，表示順序上 a 等於 b、a 大於 b 及 a 小於 b，<=> 操作左邊若大於右邊會傳回 1，等於右邊會傳回0，小於右邊會傳回 -1，在 Ruby 區塊中，最後一個運算的結果會作為傳回值，因此第二個 sort 範例就完成陣列中反序排列。

>> array1 = [1, 2, 3]
=> [1, 2, 3]
>> array1 << [4, 5, 6]
=> [1, 2, 3, [4, 5, 6]]
>> array2 = [1, 2, 3]
=> [1, 2, 3]
>> array2 + [4, 5, 6]
=> [1, 2, 3, 4, 5, 6]

>> array1 = [[1, 2, 3], [4, 5, 6]]
=> [[1, 2, 3], [4, 5, 6]]
>> array2 = array1 * 2
=> [[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]
>> array2[0][0] = 100
=> 100
>> array2
=> [[100, 2, 3], [4, 5, 6], [100, 2, 3], [4, 5, 6]]
>> array1
=> [[100, 2, 3], [4, 5, 6]]

* 也可以用來拆解陣列，如果你要將陣列中的元素一次指定給數個變數時會很方便：
>> x, y, z = *[10, 20, 30]
=> [10, 20, 30]
>> x
=> 10
>> y
=> 20
>> z
=> 30
>> x, y, z = [40, 50, 60]
=> [40, 50, 60]
>> x
=> 40
>> y
=> 50
>> z
=> 60

* 也可以用來收集資料為陣列，例如:
>> *a = 1, 2, 3
=> [1, 2, 3]
>> a = [1, 2, 3]
=> [1, 2, 3]
>> a = 1, 2, 3
=> [1, 2, 3]

>> x = 10
=> 10
>> y = 20
=> 20
>> x, y = y, x # swap x,y
=> [20, 10]
>> x
=> 20
>> y
=> 10

=> ["Justin", "caterpillar"]
>> users = ["momor", "hamini", "Justin"]
=> ["momor", "hamini", "Justin"]
=> ["Justin"]
=> ["Justin", "caterpillar", "momor", "hamini"]
=> ["caterpillar"]

>> array = ["one", "two", "three", "four"]
=> ["one", "two", "three", "four"]
>> array[0]
=> "one"
>> array[0] = 1
=> 1
>> array
=> [1, "two", "three", "four"]
>> array[1..3]
=> ["two", "three", "four"]
>> array[1...3]
=> ["two", "three"]
>> array[1..2] = [2, 3]
=> [2, 3]
>> array
=> [1, 2, 3, "four"]
>> array[-1]
=> "four"
>> array[-2]
=> 3
>> array[1, 3]
=> [2, 3, "four"]
>> array[1, 9]
=> [2, 3, "four"]
>> array[1, -1]
=> [2, 3, "four"]
>> array[1..9]
=> [2, 3, "four"]
>> array[1, 3] = ["two", "three", 4]
=> ["two", "three", 4]
>> array
=> [1, "two", "three", 4]
>> array[1, 0] = [2, 3] # 在位置 1 插入 [2, 3]
=> [2, 3]
>> array
=> [1, 2, 3, "two", "three", 4]
>> array[1..0] = ["two", "three"] # 在位置 1 插入 ["two", "three"]
=> ["two", "three"]
>> array
=> [1, "two", "three", 2, 3, "two", "three", 4]

[n..m]可 指定起始索引與結束索引，且包括結束索引以取得元素，如果結束索引超出了陣列範圍，就是直接取到最後一個索引元素， [n...m]可 指定起始索引與結束索引，但包括結束索引以取得元素（實際上 n..m 或 n...m 建立了 Range 實例，之後會再說明）。[n, length]可指定起始索引與長度取得元素，如果長度超出了剩餘長度，就是直接取到最後一個元素。[n..m]、[n...m]或[n, length]右邊接上=，表示作取代的動作，如果指定了[n..0]、[n...0]或[n, 0]，則是技巧性地作了安插的動作。

>> array = Array.new(5)
=> [nil, nil, nil, nil, nil]
>> array = Array.new(5, 10)
=> [10, 10, 10, 10, 10]
>> array = Array.new(5, [1, 2, 3])
=> [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
>> array[0][0] = "one"
=> "one"
>> array
=> [["one", 2, 3], ["one", 2, 3], ["one", 2, 3], ["one", 2, 3], ["one", 2, 3]]

Array.new 第一個參數為陣列長度，第二個為每個索引預設元素，如果沒有指定則預設為nil，注意 Array.new(5, [1, 2, 3]) 的陷阱，實際上五個索引都會參考至同一個 [1, 2, 3] 物件，如果你希望五個索引參考至不同的陣列物件，必須如下:

>> %w[Python JavaScript Ruby]
=> ["Python", "JavaScript", "Ruby"]

>> %w
=> ["Python", "JavaScript", "Ruby"]
>> %w|Python JavaScript Ruby|
=> ["Python", "JavaScript", "Ruby"]

Supplement
tutorialpoint - Arrays
Ruby arrays are ordered, integer-indexed collections of any object. Each element in an array is associated with and referred to by an index...

Rubylearning.com - Arrays

### [NodeJS 文章收集] NodeJS accessing file with relative path

Source From  Here Question It seemed like a straight forward problem. But I am not able to crack this. Within  helper1.js  I would like to a...