LoginSignup
1
0

More than 5 years have passed since last update.

Rubyのハッシュ再確認

Posted at

ハッシュとは

簡単に言えば、である
その表にはKeyValueが存在する
ex).

Key Value
果物 個数
りんご 20
オレンジ 13
ぶどう 17

Rubyハッシュの初期化

ハッシュオブジェクト作成

hash = {"りんご" => 20, "オレンジ" => 13, "ぶどう" => 17}

2次元のハッシュの初期化

hash = Hash.new{|h,k| h[k] = {} }

カウントなどに使用したい場合0に初期化する必要があるので{}の中に記述する

hash = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k]=0}}

ハッシュ取り出し

値を表示する

puts hash["りんご"]

-> 20

1
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
1
0