Live data from Hacker News

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

blog.peepcode.com

41–50 of 95 posts

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

#41
Interesting to see this convergence of syntax. Unfortunately, it's not true that you don't have to learn a "new syntax." As often with Ruby libraries, the devil is in the subtleties.

This does not work (although I think it could):

{"foo bar": 'b'}

but this does:

{:"foo bar" => 'b'}

Without being truly JSON compatible, it's only moderately useful.

One annoyance of Ruby for me has always been that many libraries, such as Rails, allow for symbols or strings interchangeably - but only in "most" cases, often leading to head banging in the other cases.

More intrigue: the syntaxes are also mixable

{foo: 'bar', "add-a" => 'dash', :'or a' => 'space in a symbol'}

is valid.

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

#42
post #38
post #37

Earlier quoted context omitted.

It will also make your code drastically slower.

Slower than what alternative?

Slower than my_dict = { "foo": 1, "baz": 2 }

I was unaware that there was a difference in performance, but a quick timing check shows that it takes about twice as long to create a dictionary with 7 items using dict() than using {}.

It makes sense when I stop to think about what the code would have to do, though.

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

#44
post #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 ke…

oh really?

Are you a Ruby dev?

If so, is your concern based on the impact this has on you personally, or is it based on concern for some mythical other dev who might have problems with the additional "cognitive cost"?

I've been a developing in Ruby now for about 4 years, I think this (the new syntax) is absolutely the right thing to do

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

#45
post #28

Earlier quoted context omitted.

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.

array() isn't a function, it's just PHP array literal syntax.

    $x = array("a" => 100); // legal
    $y = my_func("a" => 100); // illegal

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

#47
post #9

Earlier quoted context omitted.

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…

Combining Haml with jQuery Mobile makes it pretty much mandatory:

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

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

#48
post #9

Earlier quoted context omitted.

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…

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

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

#49
post #38

Earlier quoted context omitted.

Slower than what alternative?

Slower than my_dict = { "foo": 1, "baz": 2 } I was unaware that there was a difference in performance, but a quick timing check shows that it takes about twice as long to create a dictionary with 7 items using dict() than using {}. It makes sense when I stop to think about what the code would have to do, though.

You're right; I'm seeing dict() with explicit kwargs taking 3 times as long as a literal. My unscientific and non-rigorous benchmarks tell me that the construction and unpacking of the kwargs in the dict() call are the cause of most of the slowdown.

With the time it takes to assign with a literal normalized to 1, I'm seeing dict(kwargs) (with kwargs created before the benchmark) take 2 and dict(key_1=val_1, key_2=val_2, etc.) take 3.

Post reply on HN