Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Hash Rocket deprecated?

The well-cited RIP Hash rocket post would seem to imply the Hash Rocket syntax (:foo => "bar") is deprecated in favor of the new-to-Ruby JSON-style hash (foo: "bar"), but I can't find any definitive reference stating the Hash Rocket form is actually deprecated/unadvised as of Ruby 1.9.

like image 792
mahemoff Avatar asked Apr 04 '12 02:04

mahemoff


People also ask

What is a Hash Rocket?

Fat comma (also termed hash rocket in Ruby and a fat arrow in JavaScript) refers to a syntactic construction that appears in a position in a function call (or definition) where a comma would usually appear.

How do you define a hash in Ruby?

In Ruby you can create a Hash by assigning a key to a value with => , separate these key/value pairs with commas, and enclose the whole thing with curly braces.


1 Answers

The author of that blog post is being overly dramatic and foolish, the => is still quite necessary. In particular:

  1. You must use the rocket for symbols that are not valid labels: :$set => x is valid but $set: x is not. In Ruby 2.2+ you can get around this problem with quotes: '$set': x will do The Right Thing.

  2. You must use the rocket if you use keys in your Hashes that aren't symbols, such as strings, integers or constants. For example, 's' => x is valid but 's': x is something completely different.

You can kludge around the above in the obvious manner of course:

h = { } h[:'where.is'] = 'pancakes house?' # etc. 

but that's just ugly and unnecessary.

The rocket isn't going anywhere without crippling Ruby's Hashes.

like image 175
mu is too short Avatar answered Sep 17 '22 03:09

mu is too short