Questions tagged [hashrocket]

The `=>` symbol for building hash literals in Perl and Ruby.

The hash rocket is the => symbol that is used to build hash literals in Perl:

my %h = ( a => 'b' )

and Ruby:

h = { :a => 'b' }

The hash rocket is also known as a fat comma in the Perl world as it behaves similarly to a comma that stringifies its left side.

16 questions
141
votes
5 answers

Is there any difference between the `:key => "value"` and `key: "value"` hash notations?

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?
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
106
votes
1 answer

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…
mahemoff
  • 44,526
  • 36
  • 160
  • 222
55
votes
5 answers

In Ruby what does "=>" mean and how does it work?

While learning Ruby I've come across the "=>" operator on occasion. Usually I see it in the form of :symbol => value and it seems to be used frequently when passing values to functions. What exactly is that operator called? What does it do/mean?…
Dustin Martin
  • 1,297
  • 2
  • 12
  • 22
43
votes
2 answers

What's the difference between colon ":" and fat arrow "=>"

What's the difference between colon : and fat arrow => in Ruby? Or when to use what? :foo => true foo: true
John Base
  • 457
  • 1
  • 4
  • 6
7
votes
3 answers

What's the syntax (=>) used in assign error object to variable of `rescue` method?

The rescue which could assigns a variable to reference the error object has this syntax (=>) rescue => e If rescue is the one of the general method call, what's the meaning of =>. Could I use the same syntax on other method call? my_method arg1,…
steveyang
  • 9,178
  • 8
  • 54
  • 80
5
votes
2 answers

What does the "=>" in "rescue Exception => e" do?

Given the example: def method_of_doom my_string = "I sense impending doom." my_string.ah_ha_i_called_a_nonexistent_method rescue NoMethodError => e: puts "PROBLEM: " + e.to_s rescue Exception: puts "Uhh...there's a problem with that there…
Jeremy Iglehart
  • 4,281
  • 5
  • 25
  • 38
2
votes
1 answer

javascript object in rocket syntax

I want to get a value from this hash in JavaScript: hash= {:user_authenticated=>false, :user_email=>"nope"} hash.user_authenticated hash[user_authenticated] hash["user_authenticated"] hash[:user_authenticated] Nothing seems to work. I get…
rikkitikkitumbo
  • 954
  • 3
  • 17
  • 38
2
votes
2 answers

after_commit block executes when parameter uses hashrocket syntax, but not when it uses colon syntax

I have a model that's using activerecord lifecycle callbacks pretty heavily. I'm using the after_commit callback to execute sidekiq jobs that require a primary key to run, on create. after_commit on: :create do async_process end The code inside…
dfried
  • 23
  • 3
2
votes
1 answer

Notation "name: " vs ":name => "

I'm not too clear what's the difference between the follwing two notations: = render :partial => "order_fields", :locals => { :t => type, :f => c} = render :partial => "order_fields", :locals => { t: type, f: c} They both seem to work but I can't…
Don Giulio
  • 2,946
  • 3
  • 43
  • 82
2
votes
1 answer

rubymine complaining about rails controller scaffold syntax

I'm scaffolding with rails, and the following code is being generated format.json { render json: @leg, status: :created, location: @leg } But RubyMine is complaining unless I switch it to format.json { render :json => @leg, :status => :created,…
Chris Barry
  • 4,564
  • 7
  • 54
  • 89
1
vote
2 answers

Ruby exception hashrocket; is there an alternative json-styled way to catch an exception?

So, for hashes in Ruby you can use hashrockets like this: corned_beef = { :ingredient1 => "beef", :ingredient2 => "potatoes" } or the more concise json-ish style. corned_beef = { ingredient1: "beef", ingredient2: "potatoes" } Is…
Michael K Madison
  • 2,242
  • 3
  • 21
  • 35
1
vote
2 answers

How to automatically evaluate expressions after hash rockets in Sublime Text

In the Ruby Kickstart tutorial ('05:10), when a hash rocket # => is typed within the text editor, it shows what the expression will evaluate to. For example: 2 + 3 # => 5 I am using Sublime Text 2, though cannot find how to get this to work.…
1
vote
2 answers

How to force Rails 4 to use old-style hash syntax

I've just upgraded an old project to Rails 4 and I've just realized that it has upgrade the schema.rb using the new-style hash syntax. I suppose Rails is gonna use this syntax for all its generators. How can I, friendly, say to Rails that I prefer…
fguillen
  • 36,125
  • 23
  • 149
  • 210
1
vote
1 answer

Ruby/Rails hash rockets Syntax

Can someone point me to a good primer just explaining the different syntactic features in Ruby/Rails? For instance, how come some examples I see do myMethod(x: "z") and others do myMethod(:x => "x")? The syntax in general seems strange to me, just…
Josh M.
  • 26,437
  • 24
  • 119
  • 200
-1
votes
4 answers

What is the difference between writing `:name => "String"` and `name: "String"`?

Is there a difference between writing parameter: "String" and the lengthier :parameter => "String"
1
2