Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6998419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:24:34+00:00 2026-05-27T20:24:34+00:00

Is there any difference between :key => value (hashrocket) and key: value (Ruby 1.9)

  • 0

Is there any difference between :key => "value" (hashrocket) and key: "value" (Ruby 1.9) notations?

If not, then I would like to use key: "value" notation. Is there a gem that helps me to convert from :x => to x: notations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:24:35+00:00Added an answer on May 27, 2026 at 8:24 pm

    Yes, there is a difference. These are legal:

    h = { :$in => array }
    h = { :'a.b' => 'c' }
    h[:s] = 42
    

    but these are not:

    h = { $in: array }
    h = { 'a.b': 'c' } # but this is okay in Ruby2.2+
    h[s:] = 42
    

    You can also use anything as a key with => so you can do this:

    h = { C.new => 11 }
    h = { 23 => 'pancakes house?' }
    

    but you can’t do this:

    h = { C.new: 11 }
    h = { 23: 'pancakes house?' }
    

    The JavaScript style (key: value) is only useful if all of your Hash keys are “simple” symbols (more or less something that matches /\A[a-z_]\w*\z/i, AFAIK the parser uses its label pattern for these keys).

    The :$in style symbols show up a fair bit when using MongoDB so you’ll end up mixing Hash styles if you use MongoDB. And, if you ever work with specific keys of Hashes (h[:k]) rather than just whole hashes (h = { ... }), you’ll still have to use the colon-first style for symbols; you’ll also have to use the leading-colon style for symbols that you use outside of Hashes. I prefer to be consistent so I don’t bother with the JavaScript style at all.

    Some of the problems with the JavaScript-style have been fixed in Ruby 2.2. You can now use quotes if you have symbols that aren’t valid labels, for example:

    h = { 'where is': 'pancakes house?', '$set': { a: 11 } }
    

    But you still need the hashrocket if your keys are not symbols.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When dealing with a collection of key/value pairs is there any difference between using
Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
Is there any difference between: if foo is None: pass and if foo ==
Is there any difference between a volatile Object reference and AtomicReference in case I
Is there any difference between website and web application project? What if you are
is there any difference between: lock((IDictionary) _collection).SyncRoot) or lock(_collection)
Is there any difference between SqlCommand.CommandTimeout and SqlConnection.ConnectionTimeout in .NET?
Is there any difference between these tow pieces of code & which approach is
Is there any difference between type casting & type conversion in c++.
Is there any difference between these two LINQ statements: var query = from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.