Search code examples
ruby-on-railsrubyhashsymbols

How to convert ruby hash_sym from hash arrow to hash colon


In Rails: I like to convert

my_arrow_hash = {:a=>1,:b=>2,:c=>3}

to

My_colon_hash = {a: 1, b: 2, c: 3}


Solution

  • The two are identical. They both declare a hash with keys that are ruby symbols :a, :b, and :c.

    The first declaration ({:a=>1,:b=>2,:c=>3}) uses the traditional ruby syntax to declare the hash.

    The second declaration ({a: 1, b: 2, c: 3}) uses a newer and shorter syntax that was introduced in Ruby 1.9 and can only be used when the keys are ruby symbols.