tl;dr
Array クラスのメソッドをちょろっと触ってみたメモ。
参考
メモ
delete_if メソッドと reject! メソッド
- 要素の数だけ繰り返しブロックを実行してブロックの戻り値が真になった要素を削除
$ pry
[1] pry(main)> words = ["a", "b", "c", "d", "e"]
=> ["a", "b", "c", "d", "e"]
[2] pry(main)> words.delete_if do |item| item =~ /^[ace]/ end
=> ["b", "d"]
reject!
メソッドも同様の結果となる。
$ pry
[1] pry(main)> words = ["a", "b", "c", "d", "e"]
=> ["a", "b", "c", "d", "e"]
[2] pry(main)> words.reject! do |item| item =~ /^[ace]/ end
=> ["b", "d"]
但し、delete_if
と reject!
メソッドではレシーバ自体に変化が無かった(要素が削除されなかった)場合の挙動が以下のように異なる。
$ pry
[1] pry(main)> words = ["a", "b", "c", "d", "e"]
=> ["a", "b", "c", "d", "e"]
[2] pry(main)> words.delete_if do |item| item =~ /^[xyz]/ end
=> ["a", "b", "c", "d", "e"]
[3] pry(main)> words.reject! do |item| item =~ /^[xyz]/ end
=> nil
[4] pry(main)> p words
["a", "b", "c", "d", "e"]
each_with_index
- 要素の数だけブロックを繰り返し実行
- ブロック引数には各要素と番号が入る
$ pry
[1] pry(main)> a = ["hage", "pika", "tsuru"]
=> ["hage", "pika", "tsuru"]
[2] pry(main)> a.each_with_index { |atama, i| print i, ":", atama, "\n" }
0:hage
1:pika
2:tsuru
=> ["hage", "pika", "tsuru"]
shift / unshift / push
- shift ... 配列の最初の要素を削除し、その要素を返す
- unshift ... 配列の先頭に要素を追加(引数が無い場合には何もしない)
- push ... 配列の末尾に要素を追加
$ pry
[1] pry(main)> words = ["one", "two", "three"]
=> ["one", "two", "three"]
[2] pry(main)> words.shift
=> "one"
[3] pry(main)> words.shift
=> "two"
[4] pry(main)> words.unshift
=> ["three"]
[5] pry(main)> words.push("four")
=> ["three", "four"]
[6] pry(main)> p words
["three", "four"]
=> ["three", "four"]
[7] pry(main)> words.unshift("two")
=> ["two", "three", "four"]
concat / compact / uniq
- concat ... 要素を連結
- compact ... nil を削除(非破壊的メソッド)
- uniq ... 重複している要素を削除(非破壊的メソッド)
$ pry
[1] pry(main)> a = [:a, :a, :b, :c]
=> [:a, :a, :b, :c]
[2] pry(main)> a[5] = :e
=> :e
[3] pry(main)> a.concat([:a, :b, :c])
=> [:a, :a, :b, :c, nil, :e, :a, :b, :c]
[4] pry(main)> a.compact
=> [:a, :a, :b, :c, :e, :a, :b, :c]
[5] pry(main)> a.uniq
=> [:a, :b, :c, nil, :e]
[6] pry(main)> p a
[:a, :a, :b, :c, nil, :e, :a, :b, :c]
=> [:a, :a, :b, :c, nil, :e, :a, :b, :c]
find / detect
- find ... 要素を探して取り出し、ブロックが真となったら処理を中断する(該当する要素があれば処理を中断する)
- detect ... find の別名
$ pry
[1] pry(main)> words = ["ab", "bc", "cd", "de"]
=> ["ab", "bc", "cd", "de"]
[2] pry(main)> words.find do |word| word =~ /c/ end
=> "bc"
[3] pry(main)> words.detect do |word| word =~ /c/ end
=> "bc"
パーセント記法
以下の記事がとても参考になった。
blog.toshimaru.net
slice
$ pry
[1] pry(main)> a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
[2] pry(main)> p a.slice(1,3)
[2, 3, 4]
=> [2, 3, 4]
[3] pry(main)> p a.slice(1..3)
[2, 3, 4]
=> [2, 3, 4]
index
- 文字列の中に第一引数で指定した文字が含まれていれば、その開始位置を整数で返す
- 第二引数が指定されている場合、その位置から検索する
$ pry
[1] pry(main)> p "oe!oe!oe!oe!oe!oe".index("!", 3)
5
=> 5
[2] pry(main)> p "oe!oe!oe!oe!oe!oe".index("!")
2
=> 2
chop / chomp
- chop ... 末尾の一文字を削除(非破壊的)
- chomp ... 末尾の改行を削除(非破壊的)(※改行が無ければ何もしない)
[3] pry(main)> x = "Hello world.\n"
=> "Hello world.\n"
[4] pry(main)> x.chop
=> "Hello world."
[5] pry(main)> x.chop
=> "Hello world."
[6] pry(main)> x.chomp
=> "Hello world."
[7] pry(main)> p x
"Hello world.\n"
=> "Hello world.\n"
〆
引き続き、色々とメモる予定。