Arrayのインスタンスメソッド

&, |

irb(main):001:0> a = [1,1,3,5]
=> [1, 1, 3, 5]
irb(main):002:0> b = [1,2,3]
=> [1, 2, 3]
irb(main):003:0> a&b
=> [1, 3]
irb(main):004:0> a|b
=> [1, 3, 5, 2]

assoc,rassoc

irb(main):005:0> s1 = ["colors", "red", "blue", "green"]
=> ["colors", "red", "blue", "green"]
irb(main):006:0> s2 = ["letters", "a","b","c"]
=> ["letters", "a", "b", "c"]
irb(main):007:0> s3 = "foo"
=> "foo"
irb(main):008:0> a = [s1, s2, s3]
=> [["colors", "red", "blue", "green"], ["letters", "a", "b", "c"], "foo"]
irb(main):009:0> a.assoc("letters")
=> ["letters", "a", "b", "c"]
irb(main):010:0> a.assoc("foo")
=> nil
irb(main):011:0> a.assoc("colors")
=> ["colors", "red", "blue", "green"]
irb(main):012:0> a.assoc("red")
=> nil
irb(main):013:0> a.rassoc("red")
=> ["colors", "red", "blue", "green"]
irb(main):014:0> a.rassoc("a")
=> ["letters", "a", "b", "c"]
irb(main):015:0> a.rassoc("b")
=> nil
irb(main):016:0> a.rassoc("foo")
=> nil
正直つかったことない。

at

irb(main):017:0> a.at(1)
=> ["letters", "a", "b", "c"]
irb(main):008:0> a.at(1..2)
TypeError: can't convert Range into Integer
from (irb):8:in `at'
from (irb):8
from :0
atは範囲を引数に取れない。

delete

引数の値と同じ値の要素を削除する。
irb(main):026:0* a = [["a", "b", "c", "d"]
irb(main):027:1> ]
=> "a", "b", "c", "d"
irb(main):028:0> a.delete("b")
=> nil
irb(main):029:0> a
=> "a", "b", "c", "d"
irb(main):030:0> a.flatten!
=> ["a", "b", "c", "d"]
irb(main):031:0> a.delete("c")
=> "c"
irb(main):032:0> a
=> ["a", "b", "d"]
irb(main):033:0> a.delete("x") { "not found!"}
=> "not found!"
irb(main):034:0> a.delete("a") { "not found!"}
=> "a"
ブロックを渡すと、見つからなかった場合にブロックの戻り値を返す。

fill

irb(main):038:0> a = ["a","b","c","d"]
=> ["a", "b", "c", "d"]
irb(main):040:0> a.fill("x")
=> ["x", "x", "x", "x"]
irb(main):041:0> a.fill("z", 2, 2)
=> ["x", "x", "z", "z"]
irb(main):042:0> a.fill("z", 0..1)
=> ["z", "z", "z", "z"]
irb(main):043:0> a.fill("x", 0..1)
=> ["x", "x", "z", "z"]
irb(main):044:0> a.fill("y", 1, 4)
=> ["x", "y", "y", "y", "y"]

index

irb(main):054:0> a.index("y")
=> 1
irb(main):055:0> a.index("x")
=> 0
irb(main):056:0> a.index("y")
=> 1
irb(main):057:0> a.index("z")
=> nil
見つからなかったらnilを返す。

rindex

irb(main):041:0> a
=> ["a", "b", "c", "a", "b", "c"]
irb(main):042:0> a.rindex("c")
=> 5

indexes

irb(main):001:0> a = ["x", "y", "y", "y", "y"]
=> ["x", "y", "y", "y", "y"]
irb(main):058:0> a.indexes(1,2,3)
(irb):58: warning: Array#indexes is deprecated; use Array#values_at
=> ["y", "y", "y"]
irb(main):059:0> a.indexes(1..3)
(irb):59: warning: Array#indexes is deprecated; use Array#values_at
=> "y", "y", "y"
あれ、1.8.5では非推奨か。引数が範囲だと配列の配列を返してるし。

values_at

irb(main):001:0> a = ["x", "y", "y", "y", "y"]
=> ["x", "y", "y", "y", "y"]
irb(main):003:0> a.values_at(0..3)
=> ["x", "y", "y", "y"]
irb(main):004:0> a.values_at(0,1,2,3)
=> ["x", "y", "y", "y"]

reverse_each

irb(main):031:0> a
=> ["a", "b", "c"]
irb(main):035:0> a.reverse_each{|x| puts x}
c
b
a
=> ["a", "b", "c"]

slice

irb(main):041:0> a
=> ["a", "b", "c", "a", "b", "c"]
irb(main):043:0> a.slice(1..2)
=> ["b", "c"]
irb(main):044:0> a
=> ["a", "b", "c", "a", "b", "c"]
irb(main):045:0> a[1..2]
=> ["b", "c"]
Array#[]の別名。