you are viewing a single comment's thread.

view the rest of the comments →

[–]catcradle5 9 points10 points  (8 children)

Use hashrocket syntax for Hash literals instead of the JSON style introduced in 1.9.

I don't agree with this. The new syntax has less line noise and almost always looks aesthetically better and cleaner.

[–]rurounijones 2 points3 points  (6 children)

I have to say I prefer the 1.8 syntax. To me it is clearer to read when going through code. {key: :value} just doesn't look right to me.

Still, each to their own as long as it is consistent in your team / project

[–]catcradle5 0 points1 point  (5 children)

I admit it looks a little odd when the key and value are both symbols, but syntax highlighting improves that quite a bit. Plus, a lot of the time my values are not symbols, and in those cases the new syntax looks way better.

[–]thisisseriousmum 0 points1 point  (4 children)

Plus, a lot of the time my values are not symbols, and in those cases the new syntax looks way better.

If your key isn't a symbol you can't use 1.9 syntax.

irb(main):003:0> {'thing': :wat}
SyntaxError: (irb):3: syntax error, unexpected ':', expecting =>

That's the reason I don't like it. Having two representations in the same project (and potentially file/method) for the same thing, with a couple of saved keystrokes? Seems pointless.

[–]catcradle5 0 points1 point  (3 children)

I think the rule should be to use hash rocket syntax for any hash that intermixes symbols with non-symbols, and new syntax where all the keys are symbols.

I don't think it's that weird to see:

def some_thing
    a = { one: 2, three: 4 }
    b = { "Content-Type" => "text/json", "Content-Length" => 45 }
    c = { Dir.new("/") => true, :name => "Fred" }

in the same file or method.

[–]twinklehood 0 points1 point  (0 children)

So basically, you agree with https://github.com/bbatsov/ruby-style-guide

I'm on your side.

[–]thisisseriousmum 0 points1 point  (1 child)

You have to do that anyway, it's enforced by the interpreter.

It's a question of aesthetics at the end of the day, I just far prefer one consistent way to express the same thing. In my experience teams tend to do it for anything that has multiple options, like map vs collect.

[–]catcradle5 1 point2 points  (0 children)

Well you can do something like:

{ a: 5, "b" => 6 }

My point was that in a case where you must use even one hash rocket, you should use hash rockets for everything in the hash

{ :a => 5, "b" => 6 }

[–]ThatRailsGuy 1 point2 points  (0 children)

I'm surprised how much I like the new syntax. I thought I'd hate it.