LoginSignup
2
2

More than 3 years have passed since last update.

RubyのArrayクラスのメソッドをたくさん使ってみた <part 1>

Last updated at Posted at 2019-12-06

Rubyで配列を操作していた際に、知らないメソッドがたくさんあったので片っ端から調べて見ようと思った。

Arrayクラスのメソッド一覧を表示する

p array.methods

Arrayクラスのメソッド一覧

[:each_index, :join, :rotate, :rotate!, :sort!, :sort_by!, :collect!, :map!, :select!, :keep_if, :values_at, :delete_at, :delete_if, :to_h, :reject!, :transpose, :assoc, :include?, :fill, :uniq!, :rassoc, :flatten, :compact, :flatten!, :shuffle, :sample, :permutation, :combination, :repeated_permutation, :shuffle!, :compact!, :product, :repeated_combination, :bsearch_index, :bsearch, :&, :*, :+, :-, :sort, :count, :find_index, :select, :reject, :collect, :map, :first, :any?, :pack, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle, :sum, :uniq, :|, :insert, :index, :<=>, :<<, :clear, :rindex, :replace, :==, :[], :[]=, :empty?, :eql?, :reverse, :reverse!, :concat, :max, :min, :inspect, :length, :size, :each, :delete, :to_ary, :slice, :slice!, :to_a, :to_s, :dig, :hash, :frozen?, :at, :fetch, :last, :push, :pop, :shift, :unshift, :find, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :one?, :none?, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :slice_after, :slice_when, :chunk_while, :lazy, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :singleton_method, :instance_variable_defined?, :define_singleton_method, :method, :public_method, :instance_variable_set, :extend, :to_enum, :enum_for, :===, :=~, :!~, :respond_to?, :freeze, :object_id, :send, :display, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]

こんなにたくさんあったのか...

と、いうことで上から最初から順に調べて使ってみた。破壊的メソッドとそうでないものが、混ざっていますがお気になさらず。。

1.length

配列の要素の数を整数で返す。
上のメソッド一覧では一番上にはないが、メソッドの数を数えるために先頭に持ってきた。

Arrayクラスのメソッドの個数を以下のように数えることができる(array.methodsがArrayクラス)

p array.methods.length
177

2.each_index

配列の要素の数だけブロックを繰り返し実行する。繰り返しごとにブロック引数には各要素のインデックス(位置)の整数が入る。

array = [1,2,3]
new_array = array.each_index do |idx|
   "#{idx}. #{array[idx]}"
end

p new_array
[1, 2, 3]

3.join

配列の各要素を文字列に変換し、引数sepを区切り文字として結合した文字列を返す。

array = [1,2,3]
p array.join(",")
"1,2,3"

4.rotate

要素を回転させるように前後に移動した新しい配列を返す。

array = [1,2,3,4,5]
p array.rotate(2)
p array.rotate(-1)
[3, 4, 5, 1, 2]
[5, 1, 2, 3, 4]

5.sort

配列の要素をソートした新しい配列を返す。

array = [2,1,7,9,3]
p array.sort
[1, 2, 3, 7, 9]

6.sort_by!

ブロックを使って要素をソートする。

以下の例は配列の要素の文字列を短い順に並べ替える例

array = ["apple", "orange", "dog", "cake"]
array.sort_by! do |item|
  item.size
end

p array
["dog", "cake", "apple", "orange"]

7.collect

要素の数だけ繰り返しブロックを実行し、ブロックの戻り値を集めた配列を作成して返す。

array = [1,2,3]
new_array = array.collect do |item|
   item * 2
end

p new_array
[2, 4, 6]

8.map

collectメソッドの別名

9.size

lengthメソッドの別名

10.select!

要素の数だけ繰り返しブロックを実行し、ブロックの戻り値が真になった要素を残し、偽になった要素を削除する。

array = [1,2,3,4,5]
array.select! do |item|
   item % 2 == 0
end

p array
[2, 4]

11.keep_if

要素の数だけ繰り返しブロックを実行し、ブロックの戻り値が真になった要素を残し、偽になった要素を削除する。

array = [1,2,3,4,5]
array.keep_if do |item|
   item % 2 == 0
end

p array
[2, 4]

12.values_at

配列から引数で指定した位置の要素を取り出し、配列にして返す。

array = [1,2,3,4,5]
p array.values_at(0,1,3)
p array.values_at(0..3)

[1, 2, 4]
[1, 2, 3, 4]

13.delete_at

配列から引数indexの位置の要素を削除する。

array = [1,2,3,4,5]
array.delete_at(2)

p array
[1, 2, 4, 5]

14.delete_if

要素の数だけ繰り返しブロックを実行し、ブロックの戻り値が真になった要素を削除する。

array = [1,2,3,4,5]
array.delete_if do |item|
  item % 2 == 0
end

p array
[1, 3, 5]

15.reject!

要素の数だけ繰り返しブロックを実行し、ブロックの戻り値が真になった要素を削除する。

array = [1,2,3,4,5]
array.reject! do |item|
  item % 2 == 0
end

p array
[1, 3, 5]

16.transpose

配列の配列を行と列からなるデータと見立てて、行と列を入れ替えた配列の配列を作成して返す。

array = [[1,2],[3,4]]
p array.transpose
[[1, 3], [2, 4]]

18.assoc

配列の配列を連想配列のように扱い、キーに対応する配列を取り出す。配列の配列の中で、最初の要素が引数keyと同じ配列を探して返す。

array = [[1,2],[3,4],[5,6]]
p array.assoc(3)
[3, 4]

19.include?

配列の要素に引数objが含まれていればtrue、なければfalseを返す。

array = [1,2,3,4,5]
p array.include?(1)
p array.include?(100)
true
false

20.fill

配列のすべての要素を引数objで置き換える。

array = [1,2,3,4,5]
p array.fill(1000)
[1000, 1000, 1000, 1000, 1000]

21. uniq

配列の中で重複する要素を削除した新しい配列を返す。

array = [1,2,3,4,5,4]
p array.uniq
[1, 2, 3, 4, 5]

22.rassoc

配列の配列を連想配列のように扱い、キーの値に対応する配列を取り出す。配列の配列の中で、2番目の要素が引数valと同じ配列を探して返す。

array = [[1,2],[3,4],[5,6]]
p array.rassoc(4)
[3, 4]

23. flatten

配列の配列を平坦化した新しい配列を返す。

array = [[1,2],[3,4],[5,6]]
p array.flatten
[1, 2, 3, 4, 5, 6]

24.compact

配列からnilである要素を取り除いた新しい配列を返す。

array = [1,2,3,4,5,nil,6,nil]
p array.compact
[1, 2, 3, 4, 5, 6]

25.shuffle

配列の要素の順番をランダムに入れ替えた新しい配列を返す。

array = [1,2,3,4,5,6]
p array.shuffle
[2, 4, 5, 1, 3, 6]

26.sample

配列の要素を1つランダムに返す。

array = [1,2,3,4,5,6]
p array.sample
p array.sample
p array.sample

27.permutation

配列から引数n個の要素を選んだときの順列(順序あり、重複を許さない組合せ)を数え上げる。

array = [1,2,3,4]
array.permutation(2) do |first, second|
  printf("(%d, %d) ", first, second)
end
(1, 2) (1, 3) (1, 4) (2, 1) (2, 3) (2, 4) (3, 1) (3, 2) (3, 4) (4, 1) (4, 2) (4, 3)

28.combination

配列から引数n個の要素を選んだときの組合せ(順序なし、重複を許さない組合せ)を数え上げる。

array = [1,2,3,4]
array.combination(3) do |first, second|
  printf("(%d, %d) ", first, second)
end
(1, 2) (1, 2) (1, 3) (2, 3)

このあたりで挫折したので、また追記します。。

RubyのArrayクラスのメソッドをたくさん使ってみた part 2

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2