1

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 looking for a quick at-a-glance reference to use as a cheat sheet.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • 1
    http://stackoverflow.com/questions/10004158/is-hash-rocket-deprecated?lq=1 && http://stackoverflow.com/questions/8675206/is-there-any-difference-between-the-key-value-and-key-value-hash-no?lq=1 && http://stackoverflow.com/questions/2134702/ruby-1-9-hash-with-a-dash-in-a-key?rq=1 & many other posts about the same topic: "hash rocket syntax rails" in Google – MrYoshiji Sep 16 '13 at 18:54
  • 3
    Just with regards to your specific case, if I recall correctly the 1st way of doing myMethod(x: "z") was added into ruby 1.9 while before that the only way was to use the "hashrocket" and do myMethod(:x => "x") – Althaf Hameez Sep 16 '13 at 18:54
  • here's a good general ruby sheet: http://overapi.com/ruby/ and @AlthafHameez is right about your specific question - the `x: z` syntax is just newer – dax Sep 16 '13 at 18:55
  • Thanks, I didn't even know what terminology to use to ask what these things mean. – Josh M. Sep 16 '13 at 18:56
  • 1
    Questions soliciting lists of links are off-topic on Stack Overflow. – user229044 Sep 16 '13 at 19:48

1 Answers1

8

They are the same, it is just a matter of preferences.

I also asked myself why would we add this new syntax if we already have one? Well, Programming with Ruby implies that we are lazy and want to type the less possible caracters. So this new syntax allow us - lazy programmers - to write the same thing, minus 1 caracter!


But keep in mind some stuff, like the type of the keys for instance (Ruby 1.9.3):

> {a: 12}.class
 => Hash 
> {:a => 12}.class
 => Hash 
> {'a' => 12}.keys.first.class
 => String 
> {a: 12}.keys.first.class
 => Symbol

Also, some declaration are illegal with the new syntax:

> { '1-2' => "something" }
 => {"1-2"=>"something"} 
> { 1-2: "something" }
SyntaxError: (irb):38: syntax error, unexpected ':', expecting tASSOC
{ 1-2: "something" }
      ^
(irb):38: syntax error, unexpected '}', expecting $end

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

Community
  • 1
  • 1
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • `MyModel.new(first_name: "Max")` seems to be illegal in a rails view (erb): `syntax error, unexpected ':', expecting ')'` using Rails 3.2.13. Or am I confused? – Josh M. Sep 16 '13 at 19:00
  • Are you using Ruby 1.9.x or > ? This syntax has been introduced after Ruby 1.8 – MrYoshiji Sep 16 '13 at 19:01
  • Ruby v1.8.7 - I feel like I'm missing the groundwork for Ruby/Rails. Sorry about that. I'm a life-long Java/C# developer and this is foreign/strange to me. :) – Josh M. Sep 17 '13 at 01:55
  • Okay, I see. A lot of examples use the `x: y` syntax but I'm just using an old version of Ruby! Thanks. – Josh M. Sep 17 '13 at 02:03