Live data from Hacker News

R.I.P. Ruby Hash Rocket Syntax 1993-2010

blog.peepcode.com

21–30 of 95 posts

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#21
post #9
post #3

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 ` ' irb(main):003:0> hash={"symbolwithoutspace": 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

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#23
post #10

Earlier quoted context omitted.

[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.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#25
I have to say I cried a little bit when I read this - it seems that Matz et al are trying to make the language unparseable.

(Of course, perhaps I'm more sensitive than most, having actually written an Ruby lexer - http://github.com/jasonl/eden - which made me deal with the dusty, hidden corners of the Ruby grammar.)

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#26
post #12

Am I the only one who thinks adding more syntax is a bad thing?

It's fairly terrifying what an accretion of these sorts of options did to Perl, and I say that as someone who does a lot of Perl5 coding and enjoys it.

It would be one thing to just have a new symbol that was a drop-in alternative to what works now. But in this case you have a _sometimes_ alternative, so you need to remember a new rule, if only to be able read other people's code. That cognitive cost outweighs any keystroke benefit IMHO.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#27

In related news, Hashrocket (the consultancy) is alive and well, with no intentions of changing our name to Hashcolon. http://hashrocket.com/ In fact, like almost everyone else in our space we're looking to hire => http://hashrocket.com/jobs

I was actually surprised that the Hashrocket/Ruby 1.9 syntax joke didn't come up.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#28
post #8

Cool. Now why doesn't PHP join the crowd and do this with arrays already?

I absolutely agree. The PHP array instantiation syntax is atrocious... I really dislike '=>'. While they're at it they might as well replace array() with [] and/or {}.

Yep, array() is the icing on the cake. Why is the most common data structure in the language instantiated with a function?

I could probably rant on PHP syntax all day long, but it's too off topic here.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#29
post #8

Cool. Now why doesn't PHP join the crowd and do this with arrays already?

I absolutely agree. The PHP array instantiation syntax is atrocious... I really dislike '=>'. While they're at it they might as well replace array() with [] and/or {}.

Let's throw in support for keyworded arguments while we're at it, too.
Post reply on HN