LoginSignup
1
2

More than 5 years have passed since last update.

Rubyの組み込みクラスの覚書(Hash)

Last updated at Posted at 2018-07-08

Ruby技術者認定試験Silver取得のため勉強にあたり、私が知らなかった内容をまとめます。
参考書はRuby技術者認定試験合格教本です。

Hashクラス

ハッシュの生成


> a = { "apple" => "fruit", "coffee" => "drink" }
=> { "apple" => "fruit", "coffee" => "drink" }
> a.class
=> Hash

newメソッドでブロックを取ることで、デフォルト値を設定できる


> a = Hash.new { |hash, key| hash[key] = "NONE" }
=> {}
> a["apple"]
=> "NONE"

初期値はdefaultメソッドでアクセスできる


> a = Hash.new("NONE")
=> {}
> a.default
=> "NONE"
> a.default = "Not Exist"
> a["apple"]
=> "Not Exist"

ハッシュのキーや値を取得する

values_atメソッドはキーに対応する値を配列で返す。


> a = { 1 => "a", 2 => "b", 3 => "c" }
=> { 1 => "a", 2 => "b", 3 => "c" }
> a.values_at(1, 3)
=> ["a", "c"]

fetchメソッドはキーに対応する値を返す。


> a = { 1 => "a", 2 => "b", 3 => "c" }
=> { 1 => "a", 2 => "b", 3 => "c" }
> a.featch(1)
=> "a"

キーが存在しない場合、二番目の引数が与えられていればその値を、ブロックを与えられていればそのブロックを評価した値を返す。


> a = { 1 => "a", 2 => "b", 3 => "c" }
=> { 1 => "a", 2 => "b", 3 => "c" }
> a.featch(5, "NONE")
=> "NONE"
> a.featch(5) { |key| key % 2 == 0 }
=> false

selectメソッドとfind_allメソッドは、キーと値の組み合わせについてブロックを評価して、結果が真にとなる組み合わせのみ返す。
selectメソッドではハッシュを、find_allメソッドでは配列を返す。


> a = { 1 => "a", 2 => "b", 3 => "c", 4 => "d" }
=> { 1 => "a", 2 => "b", 3 => "c", 4 => "d" }
> a.select { |key, value| key % 2 == 0 }
=> { 2 => "b", 4 => "d" } # selectはハッシュ
> a.find_all { |key, value| key % 2 == 0 }
=>  [[2, "b"], [4, "d"]] # find_allは配列

ハッシュを変更する

rejectメソッドはブロックを評価した結果が真になる値を取り除いたハッシュを生成して返す


> a = { "apple" => "fruit", "coffee" => "drink" }
=> { "apple" => "fruit", "coffee" => "drink" }
> a.reject { |key, value|  value == "drink" }
=> { "apple" => "fruit" }
> a
=> { "apple" => "fruit", "coffee" => "drink" } # もとの値は変更されない

reject!メソッドとdelete_ifメソッドはrejectメソッドの破壊的メソッド版。
ブロックを評価した結果が真になる値を取り除く。


> a = { "apple" => "fruit", "coffee" => "drink" }
=> { "apple" => "fruit", "coffee" => "drink" }
> a.reject! { |key, value|  value == "drink" }
=> { "apple" => "fruit" }
> a
=> { "apple" => "fruit" } # もとの値が変更される

replaceメソッドは、引数で与えられたハッシュで自分自身を書き換える。object_idは変わらない。


> a = { "apple" => "fruit", "coffee" => "drink" }
=> { "apple" => "fruit", "coffee" => "drink" }
> a.object_id
=> 2190908200
> a.replace({ "orange" => "fruit", "tea" => "drink" })
=> { "orange" => "fruit", "tea" => "drink" }
> a
=> { "orange" => "fruit", "tea" => "drink" }
> a.object_id
=> 2190908200

mergeメソッドは、自分自身と引数で指定されたハッシュをマージしたハッシュオブジェクトを返す。


> a = { "apple" => "foods", "coffee" => "drink" }
=> { "apple" => "foods", "coffee" => "drink" }
> a.merge({ "orange" => "fruit", "tea" => "drink", "apple" => "fruit" })
=> { "apple" => "fruit", "coffee" => "drink", "orange" => "fruit", "tea" => "drink" }
> a
> => { "apple" => "foods", "coffee" => "drink" } # もとの値は変更されない

merge!メソッドとupdateメソッドはmergeの破壊的メソッド版


> a = { "apple" => "foods", "coffee" => "drink" }
=> { "apple" => "foods", "coffee" => "drink" }
> a.merge!({ "orange" => "fruit", "tea" => "drink", "apple" => "fruit" })
=> { "apple" => "fruit", "coffee" => "drink", "orange" => "fruit", "tea" => "drink" }
> a
> => { "apple" => "fruit", "coffee" => "drink", "orange" => "fruit", "tea" => "drink" } # もとの値が変更される
1
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
1
2