4 ms·
Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?
by ericb 16y ago
Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?
- jim_h 16y agoRuby 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 16y ago[deleted]
- JonnieCache 16y agoOuch. Thanks for pointing that one out. PS. Ruby 1.9 does have a native OrderedHash, the same as the one from ActiveSupport.
- nicholaides 16y agoActually, 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 http://www.ruby-doc.org/core/classes/Hash.html
- rue 16y agoThe 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/21856 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.
- ionfish 16y agoThat's right. It is nice for DSLs though! http://beastaugh.github.com/stylish/ruby19.html http://beastaugh.github.com/stylish/ruby19.html
- laymil 16y agoCorrect. 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}
- cubicle67 16y agoMy 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
- aaronbrethorst 16y agoCombining Haml with jQuery Mobile makes it pretty much mandatory: #main_page{:"data-role" => 'page'}
- nex3 16y agoHaml keys can be plain strings. #main_page{"data-role" => 'page'} `data-` attributes are also special-cased: #main_page{data: {role: 'page'}}
- 16y ago