0

When learning rails I am often confused when in some scenarios a colon is placed before a word and on other occasions it is placed after the word. I have been reading and rereading to try an understand this better and so far have determined that when a colon is placed before the word it is a symbol.

I thought I understood this until I read "Agile Web Development with Rails 4 (Facets of Ruby), page 56".

Am I correctly understanding that a symbol has a colon before its name even when used as the key in a hash however there is an alternative syntax that places the colon after the symbol name in a hash?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Dercni
  • 1,216
  • 3
  • 18
  • 38
  • Note that the JavaScript trailing colon style doesn't not always work even when you're using symbols as Hash keys. `{ :$set => { ... } }`, for example, is valid but `{ $set: { ... } }` is a syntax error. – mu is too short Oct 30 '14 at 22:42
  • 1
    You might be interested in [**Is Hash Rocket deprecated?**](http://stackoverflow.com/q/10004158/479863), [**How to create symbol (hash key) from association, using new ruby (1.9) hash syntax?**](http://stackoverflow.com/q/8796358/479863), and [**Supporting Ruby 1.9's hash syntax in Ruby 1.8**](http://stackoverflow.com/q/9694209/479863) too. And check the *Related* sections in the sidebars for those questions for even more discussion of this confusing feature of Ruby. – mu is too short Oct 30 '14 at 22:46

2 Answers2

1

That's correct. A symbol is always defined with the colon before the name

:foo

The original notation for the Hash with symbol keys was

{ :foo => "bar" }

However, since Ruby 1.9, there is an alternative notation that was designed to be more compact.

{ foo: "bar" }

The two notations are equivalent. However, this is a specific Hash exception. The following is not a valid symbol declaration on its on

foo:
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • 1
    Thanks for the replies. What I cannot understand is if the original notation was: { :foo => "bar" } and there was a desire to shorten it why not make it: { :foo "bar" } So in essence when I see a colon before a word I know it's a symbol and when I see a colon after a word I know it's still a symbol but just being used in a hash using the compact format. i.e. Model.where(:foo => 'bar').update_all(author: 'David') – Dercni Oct 30 '14 at 22:37
  • The two notations are not equivalent since the symbols allowed with the JavaScript style are more limited than the symbols allowed with a hashrocket. – mu is too short Oct 30 '14 at 22:44
  • Thought I had this until I read the following in an example migration: t.decimal :price, precision: 8, scale: 2 So price, precision and scale are all symbols but the later two are using the compact notation within a hash?? – Dercni Oct 30 '14 at 23:01
  • and another example just to add to my confusion: <%= f.text_area :description, rows: 6 %> – Dercni Oct 30 '14 at 23:06
1

Yes, if you launch the Rails console, then run:

{ test: "ds"}.keys[0] == :test

You'll see it returns true

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Steven Spasbo
  • 647
  • 7
  • 18