LoginSignup
1
0

More than 3 years have passed since last update.

Rubyにおけるハッシュとシンボルのまとめ

Posted at

ハッシュとシンボルの書き方まとめ

 学習していく中で、ハッシュの実装方法の違いが出てきたためまとめます。

test.rb

# キーが文字列の書き方
test{"subject" => "Math" , "score" => 60}
# キーがシンボルの書き方
test1{:subject => "Math" , :score => 60}
# キーがシンボルの書き方(省略系)
test2{subject: "Math" , score: 60}

# それぞれの出力方法
puts test["subject"]
puts test1[:subject]
puts test2[:subject]

実行すると、全て同じ値が出力されました。
学習するたびに、投稿していこうと思います。。。

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