Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What's the difference between colon : and fat arrow => in Ruby? Or when to use what?

:foo => true
foo: true
like image 900
John Base Avatar asked Nov 20 '11 01:11

John Base


2 Answers

The syntax is for defining Hash key/value pairs, and the difference depends on the Ruby version.

Supported in both Ruby 1.8 and Ruby 1.9

:foo => true

Supported only in Ruby 1.9

foo: true

If you're developing in Ruby 1.9 you should probably use the syntax:

foo: true

as it appears to be the direction the community is moving in.

like image 113
JDutil Avatar answered Oct 31 '22 19:10

JDutil


The latter is the new Hash syntax introduced in 1.9. See, for example:

http://breakthebit.org/post/8453341914/ruby-1-9-and-the-new-hash-syntax

The hashes that the two lines generate are identical.

like image 36
Alex Peattie Avatar answered Oct 31 '22 20:10

Alex Peattie