Reference This is a memo for myself when I didn't understand the hash of ruby. If you make a mistake, please comment.
ruby 2.7.1
Thank you for your correction comment. "
#String and=>
hash1={"first"=>"Ichi"}
#With symbols=>
hash2={:first=>"Ichi"}
#Omitted writing when the key is a symbol
#(:"first"→:You can create the symbol first)
hash3={"first":"Ichi"}
Omitted writing when the key is a symbol
hash4={first:"Ichi"}
#With symbols:
hash5={:first:"Ichi"}
=>syntax error
--hash1 → String is the key --hash2,3,4 → Symbol is the key
#hash1
{"first"=>"Ichi"}
#hash2,3,4
{:first=>"Ichi"}
hash1["first"]
hash2[:first] #hash2,3,4 common
hoge="second"
hash5={hoge:2,hoge=>2}
hash5
=> {:hoge=>2, "second"=>2}
hash={"first"=>1,:first=>"Ichi"}
Recommended Posts