Introduction
- 超類: Object
- 包含模塊: Enumerable
New
基礎(chǔ)
ary = [1, "two", 3.0] #=> [1, "two", 3.0]
ary = Array.new #=> []
Array.new(3) #=> [nil, nil, nil] 默認(rèn)為 nil
Array.new(3, true) #=> [true, true, true]
提高
-
new
可以跟 block
Array.new(4) { Hash.new } #=> [{}, {}, {}, {}]
empty_table = Array.new(3) { Array.new(3) } #=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
- Hash 轉(zhuǎn)化為 Array
Array({:a => "a", :b => "b"}) #=> [[:a, "a"], [:b, "b"]]
{:a => "a", :b => "b"}.to_a #=> [[:a, "a"], [:b, "b"]]
注意點(diǎn)
-
new
的第二個(gè)參數(shù)谎懦,array 將填充同一個(gè) object,==這里有陷阱==
a = Array.new(2, Hash.new)
# => [{}, {}]
a[0]['cat'] = 'feline'
a # => [{"cat"=>"feline"}, {"cat"=>"feline"}]
a[1]['cat'] = 'Felix'
a # => [{"cat"=>"Felix"}, {"cat"=>"Felix"}]
# 修改第一個(gè) hash棘钞,第二個(gè)也跟著變入愧,因?yàn)槭峭粋€(gè) object
-
new
中的 block抽米, array 將填充不同的 object
a = Array.new(2) { Hash.new }
a[0]['cat'] = 'feline'
a # => [{"cat"=>"feline"}, {}]
Index
基礎(chǔ)
-
slice
arr = [1, 2, 3, 4, 5, 6]
arr[2] #=> 3
arr[100] #=> nil
arr[-3] #=> 4
-
first
两波,last
arr.first #=> 1
arr.last #=> 6
提高
-
slice
arr[2, 3] #=> [3, 4, 5]
arr[1..4] #=> [2, 3, 4, 5]
arr[1..-3] #=> [2, 3, 4]
-
at
此蜈,take
堰氓,drop
arr.at(0) #=> 1
arr.take(3) #=> [1, 2, 3] 提取前3個(gè)元素
arr.drop(3) #=> [4, 5, 6] 排除前3個(gè)元素
-
fetch
适荣,如果指定的 index 有元素就返回现柠,否則返回指定的信息
a = [ 11, 22, 33, 44 ]
a.fetch(1) #=> 22
a.fetch(-1) #=> 44
a.fetch(4, 'cat') #=> "cat" 沒有元素,返回指定的信息 cat
a.fetch(100) { |i| puts "#{i} is out of bounds" } #=> "100 is out of bounds"
注意點(diǎn)
-
slice
索引超出范圍時(shí)需要特別注意弛矛,==這里有陷阱==
a = [ "a", "b", "c", "d", "e" ]
a[5] #=> nil
a[6, 1] #=> nil
a[5, 1] #=> []
a[5..10] #=> []
Information
基礎(chǔ)
browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
browsers.length #=> 5
browsers.count #=> 5
browsers.size #=> 5
提高
browsers.empty? #=> false
browsers.include?('Konqueror') #=> false
注意點(diǎn)
Add Items
基礎(chǔ)
arr = [1, 2, 3, 4]
arr.push(5) #=> [1, 2, 3, 4, 5]
arr << 6 #=> [1, 2, 3, 4, 5, 6]
提高
arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
arr.insert(3, 'apple') #=> [0, 1, 2, 'apple', 3, 4, 5, 6]
arr.insert(3, 'orange', 'pear', 'grapefruit') #=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
注意點(diǎn)
-
push
够吩、unshift
和insert
一次可以加入多個(gè)元素
a = [ "b", "c", "d" ]
a.unshift("a") #=> ["a", "b", "c", "d"]
a.unshift(1, 2) #=> [ 1, 2, "a", "b", "c", "d"]
Remove Items
基礎(chǔ)
-
pop
arr = [1, 2, 3, 4, 5, 6]
arr.pop #=> 6
arr #=> [1, 2, 3, 4, 5]
shift
arr.shift #=> 1
arr #=> [2, 3, 4, 5]
arr.delete_at(2) #=> 4
arr #=> [2, 3, 5]
提高
-
delete
arr = [1, 2, 2, 3]
arr.delete(2) #=> 2
arr #=> [1,3]
# 有多個(gè)2,全部被刪掉了
-
compact
丈氓,刪除 nil 項(xiàng)
arr = ['foo', 0, nil, 'bar', 7, 'baz', nil]
arr.compact #=> ['foo', 0, 'bar', 7, 'baz']
arr #=> ['foo', 0, nil, 'bar', 7, 'baz', nil]
# arr 的值并沒有變化
-
uniq
废恋,合并重復(fù)項(xiàng)
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
注意點(diǎn)
-
delete
會(huì)將array中所有等于該數(shù)值的項(xiàng)都刪掉
-
compact!
和uniq!
可以改變?cè)瓉?array 的值谈秫,==但是返回值有陷阱==
[ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
[ "a", "b", "c" ].compact! #=> nil 沒有 nil 項(xiàng)會(huì)返回 nil
a = [ "a", "a", "b", "b", "c" ]
a.uniq! # => ["a", "b", "c"]
b = [ "a", "b", "c" ]
b.uniq! # => nil 沒有重復(fù)項(xiàng)會(huì)返回 nil
-
uniq!
可以跟 block
c = [["student","sam"], ["student","george"], ["teacher","matz"]]
c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
-
slice!
也可以用來刪除元素
a = [ "a", "b", "c" ]
a.slice!(1) #=> "b"
a #=> ["a", "c"]
a.slice!(-1) #=> "c"
a #=> ["a"]
a.slice!(100) #=> nil
a #=> ["a"]
Iterate Over 遍歷
基礎(chǔ)
-
each
arr = [1, 2, 3, 4, 5]
arr.each { |a| print a -= 10, " " }
# prints: -9 -8 -7 -6 -5
#=> [1, 2, 3, 4, 5] 原有 array 的值不變
-
reverse_each
,倒序遍歷
words = %w[first second third fourth fifth sixth]
str = ""
words.reverse_each { |word| str += "#{word} " }
p str #=> "sixth fifth fourth third second first "
提高
-
map
arr.map { |a| 2*a } #=> [2, 4, 6, 8, 10]
arr #=> [1, 2, 3, 4, 5]
-
map!
arr.map! { |a| a**2 } #=> [1, 4, 9, 16, 25]
arr #=> [1, 4, 9, 16, 25]
Select Items
基礎(chǔ)