Symbol Keys
In this page:
Symbol Keys
Using symbols (:name) as hash keys is the idiomatic Ruby convention, since symbols are immutable and compare faster than strings. The shorthand { name: "Alice" } syntax is specifically for symbol keys and is equivalent to { :name => "Alice" }. Mixing symbol and string keys in the same hash is legal but usually a sign of inconsistent design.
Note: { name: "Alice" } and { :name => "Alice" } are exactly equivalent -- the colon-shorthand is just newer, more concise syntax.
Example: Symbol Keys
options = { color: "red", size: "large" }
same_thing = { :color => "red", :size => "large" }
puts options == same_thing
puts options[:color]
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: