Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?


Correct. It is wonderful sugar, but somewhat limiting. Realistically, you need to know both syntaxes.

  irb(main):001:0> hash={:"symbol with space" => nil}
  => {:"symbol with space"=>nil}
  irb(main):002:0> hash={"symbol with space": nil}
  SyntaxError: (irb):2: syntax error, unexpected ':',  expecting tASSOC
  hash={"symbol with space": nil}
                          ^
	from /usr/bin/irb1.9.1:12:in `<main>'
  irb(main):003:0> hash={"symbolwithoutspace": nil}
  SyntaxError: (irb):3: syntax error, unexpected ':',  expecting tASSOC
  hash={"symbolwithoutspace": nil}
                           ^
	from /usr/bin/irb1.9.1:12:in `<main>'
  irb(main):004:0> hash={symbolwithoutspace: nil}
  => {:symbolwithoutspace=>nil}
  irb(main):005:0> hash={symbol with space: nil}
  SyntaxError: (irb):5: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
  hash={symbol with space: nil}


My own opinion is that although Ruby allows string-like symbols (like :"symbol with space"), I think they should be avoided. There may be a case for them, but I've not encountered one yet.

The new syntax works only with conventional symbols (no quotes), not with any other key type, so no integers, strings, objects etc. For everything else, you can fall back to the original syntax. You can even mix then if you really feel the need (yech!)

That said, for almost all use cases hashes use symbols as keys, and for these cases the new syntax is much cleaner and I'm glad to see it


Combining Haml with jQuery Mobile makes it pretty much mandatory:

    #main_page{:"data-role" => 'page'}


Haml keys can be plain strings.

    #main_page{"data-role" => 'page'}
`data-` attributes are also special-cased:

    #main_page{data: {role: 'page'}}


I did not know that. Thanks for the heads up, and thanks for Haml and Sass!


You can also use "HTML-style attributes" [1]

    #main_page(data-role='page')
[1] http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#ht...


> That said, for almost all use cases hashes use symbols as keys

I'd say there are more vanilla symbol hash uses by number, but the most interesting and powerful things you can do with hashes don't involve symbols for keys, imho.


Ruby 1.9 still uses hash rocket. I'm using RoR3 on 1.9.2 and I haven't changed any of my hashes.

I guess if you want to save some key strokes and you use symbols as keys you can use the new syntax.

The other cool/different thing about Hash in 1.9 is that it preserves order.


[deleted]


Actually, insertion order preservation is a 1.9 feature:

> Hashes enumerate their values in the order that the corresponding keys were inserted.

from http://www.ruby-doc.org/core/classes/Hash.html


The docs were wrong for a long time, but looking back at the issue, there was a quiet turnaround in http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/....

So, hashes can be considered ordered in 1.9. It's a mistake to corrupt the data structure, in my opinion, but apparently that boat has now sailed. Thanks for the prod to recheck and sorry about the outdated info.


Ouch. Thanks for pointing that one out.

PS. Ruby 1.9 does have a native OrderedHash, the same as the one from ActiveSupport.


That's right. It is nice for DSLs though!

http://beastaugh.github.com/stylish/ruby19.html




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: