0
0

More than 3 years have passed since last update.

ハッシュ・配列の新規追加方法

Posted at

はじめに

 最近は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での扱いを完全に忘れていた。

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