はじめに
最近はRailsフレームワークを使っているので、ついつい忘れていた学習初期の内容を改めてまとめておく。
ハッシュに新たにバリューとキーを追加する
hash = {}
hash[:name] = "やました" #ハッシュにないキーは新たに追加
hash[:name] = "かねむら" #すでにあるキーはバリューが更新される
puts hash[:name] #出力するときは、角括弧にシンボルでキー名
hash = {name: "かねむら"} #最終的なハッシュはこういう状態
配列
arrays = []
hash = {name: "かねむら"}
arrays << hash #配列にハッシュの形のまま追加する
puts arrays[0][:name]
#=>"かねむら"
puts arrays
#=>{:name=>"かねむら"}
最後に
Railsを使っていると中でハッシュを使っているんだろうなぁと思うことはあったが、Rubyでの扱いを完全に忘れていた。