Ruby: colon before or after a name?

You might see code like this:

hash = { :name => “David”, :age => 49 }

and then also see code like this:

hash = { name: “David”, age: 49 }

Why the difference in the position of the colon?

Quoting from “The Well Grounded Rubyist”:

“Ruby allows a special form of symbol representation in the hash-key position, with the colon after the symbol instead of before it and the hash separator arrow removed”

Leave a Reply

Your email address will not be published. Required fields are marked *