LoginSignup
0
0

More than 3 years have passed since last update.

【Ruby】Hashまとめ

Last updated at Posted at 2021-02-28

Hashまとめ

PaizaでHashを使うことがあったので、整理がてらまとめました。

Hashとは

  • みなさんお馴染みのKey,Value配列
    • なんだかんだでよく使う。実務でも割と。
    • 2次元配列が個人的には嫌いなので、Hashに逃げがち
  • Hash使えたら、Rubyを「完全に理解した」レベル。チョットデキルには程遠い。

Hash作成

hash = {}

要素の検索

Key検索

hash = { "Apple" => 1, "Orange" => 5 }
print hash.find {|k,v| k == "Apple"}
# { "Apple" => 1 }

Value検索

  • やり方は上と同じなので割愛。気が向いたら書く

要素の追加

hash = { "Apple" => 1, "Orange" => 5 }
hash.store( "Grape" , 3 )
print hash
# { "Apple" => 1, "Orange" => 5, "Grape" => 3 }

要素の削除

hash = { "Apple" => 1, "Orange" => 5 }
hash.delete("Apple")
print hash
# { "Orange" => 5}
0
0
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
0
0