LoginSignup
3
1

More than 1 year has passed since last update.

【Ruby】ArrayとHashの便利メソッドまとめ(随時更新)

Last updated at Posted at 2022-10-11

はじめに

RubyのArrayとHashの便利メソッドをまとめてあります。
ぜひ辞書としてご活用ください。

Array

取得([], first, last, index, select, find)

  • n番目の要素を取得
    要素数を超えるとnilが返る
array = ['red', 'blue', 'green']
p array[1]
# => 'blue'
p array[5]
# => nil
  • 後ろからn番目を取得
    要素数を超えるとnilが返る
array = ['red', 'blue', 'green']
p array[-1]
# => 'green'
p array[-5]
# => nil
  • 先頭・末尾を取得
array = ['red', 'blue', 'green']
p array.first
# => 'red'
p array.last
# => 'green'
  • indexを取得
    要素が存在しないとnilを返す
array = ['red', 'blue', 'green']
p array.index('blue')
# => 1
p array.index('yellow')
# => nil
p array.index{|e| e == 'blue' || e == 'green'}
# => 1
  • 条件で複数取得
array = [5, 4, 3, 2, 1]
p array.select{|e| e % 2 == 0}
# => [4, 2]
  • 条件で1件取得
    条件を満たす要素が存在しない場合はnilを返す
array = [5, 4, 3, 2, 1]
p array.find{|e| e % 2 == 0}
# => 4
p array.find{|e| e > 5}
# => nil

追加(push, unshift)

  • 末尾に追加
array = ['red', 'blue', 'green']
array.push('yellow')
p array
# => ['red', 'blue', 'green', 'yellow']
  • 先頭に追加
array = ['red', 'blue', 'green']
array.unshift('yellow')
p array
# => ['yellow', 'red', 'blue', 'green']

削除(pop, shift)

  • 末尾を削除
array = ['red', 'blue', 'green']
p array.pop
# => 'green'
p array
# => ['red', 'blue']
  • 先頭を削除
array = ['red', 'blue', 'green']
p array.shift
# => 'red'
p array
# => ['blue', 'green']

確認(empty?, include?)

  • 空配列かどうか
array = []
p array.empty?
# => true
  • 要素が存在するかどうか
array = ['red', 'blue', 'green']
p array.include?('red')
# => true

変形(compact, flatten)

  • nilを削除
array = ['red', nil, 'blue', nil, 'green']
p array.compact
# => ['red', 'blue', 'green']
  • 多次元配列を1次元配列にする
array = [['red', ['blue']], 'green']
p array.flatten
# => ['red', 'blue', 'green']

変換(to_h)

  • Hashに変換
    2要素の2次元配列のみ可能
array = [['apple', 'red'], ['banana', 'yellow']]
p array.to_h
# => {'apple'=>'red', 'banana'=>'yellow'}

Hash

取得([])

  • keyからvalueを取得
    keyが存在しない場合はnilが返る
hash = {'apple' => 'red', 'banana' => 'yellow'}
p hash['apple']
# => 'red'
p hash['grape']
# => nil

追加([]=)

  • keyとvalueを追加
hash = {'apple' => 'red', 'banana' => 'yellow'}
hash['grape'] = 'purple'
p hash
# => {'apple'=>'red', 'banana'=>'yellow', 'grape'=>'purple'}

削除(delete)

  • keyとvalueを削除
    keyが存在しない場合はnilが返る
hash = {'apple' => 'red', 'banana' => 'yellow'}
p hash.delete('apple')
# => 'red'
p hash
# => {'banana'=>'yellow'}
p hash.delete('grape')
# => nil

確認(empty?, key?, has_key?)

  • 空Hashかどうか
hash = {}
p array.empty?
# => true
  • keyが存在するかどうか
hash = {'apple' => 'red', 'banana' => 'yellow'}
p hash.key?('apple')
# => true
p hash.has_key?('apple')
# => true

変換(to_a)

  • Arrayに変換
    2次元配列になる
hash = {'apple' => 'red', 'banana' => 'yellow'}
p hash.to_a
# => [['apple', 'red'], ['banana', 'yellow']]
3
1
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
3
1