views:

222

answers:

8

Looking at other people's code it seems really common to include an extra space inside curly brace blocks. Is there a reason for that? To me it seems to add extra keystrokes for added ugliness. Especially when things get nested:

lambda { (1..5).map { |i| { :a => { :b => i } } } }

For some reason it just looks more concise and coherent to do:

lambda {(1..5).map {|i| {:a => {:b => i}}}}

Maybe the extra spaces are some text editor side effect or there is a historical reason or something? I haven't seen this addressed in style guides and if it's like 2 space indentation I want to follow conventions, but if there's no good reason I guess I'll just keep doing things my own way. Which do you prefer, and why?

A: 

I'm not Ruby programmer, but I tend to put extra space after brackets/parenthesis, if I have something longer between them - i.e. longer if statement or extra calculation as argument to a function.

So I would say - although I'm not Ruby programmer - do as you wish / as your employer (if you're not freelancer/hobbyist) wishes.

Adam Kiss
+1  A: 

Seems like just a matter of style. I think they're ugly, leave out the extra spaces.

Edit: I do agree with the comment below that just saying it's a matter of style doesn't give a license to do whatever you want. If anyone else is ever going to have to read or maintain the code then adhering to a common style is just good coding practice. So I'd be the first person to put in spaces if someone convinced me that most Ruby code uses spaces in this manner. Personally I still think it's ugly but that's far less important than having others be able to read my code.

Kevin Gale
Lots of things are a matter of style -- but an individual's preference on style is not the only (or even primary) factor here. The key question is how to use a style that meshes with the Ruby community. Doing so will make your code easier to read and give you more respect in the community.
David James
Being contrary here: easier to read for who? Granted if the code is designed to be READ by the Ruby community, sure -- but if the person that has to make sense of it most is you, you should set out your code in the way that makes it easiest for you to read. (That said, I'm not advocating a wildly divergent coding style -- best to try to make an effort to conform somewhat, as you'll find it pays off when you need to read others' code...)
Shadowfirebird
+4  A: 

Most of the Ruby code I see (and hopefully all the code I write) uses this style:

{ :key => 'value' }

This is what I have gotten used to and internalized.

When all is said and done, this one particular style issue is not of paramount importance. That said, the Ruby community (as do others) believes it is important to match your style (a) to the project you are working with and (b) to the community of code as a whole. That's why I recommend to use the extra white spaces.

BTW, this is a good Ruby style guide: http://www.caliban.org/ruby/rubyguide.shtml#style

David James
So I took your advice and ran a few simple regular expressions on the top watched ruby projects on github. And it turns out blocks almost always include the extra spaces while hashes usually do, but not always. Some of the combination approaches below were interesting but don't seem to be widely favored or immediately obvious, so I've decided to submit to the will of the majority and change my style to use the extra spaces (and anticipate ruby 1.9 hashes that don't need them, as Mereghost points out).
eremite
+1  A: 

The example you show is a matter of style. I think you take the spaces out if you're going to put it all on one line. However, if you put

[1,2,3].slice (2)

in your code, Ruby gives you a warning

warning: don't put space before argument parentheses

So, I guess Ruby is passionate about the parenthesis, but not the braces.

dbrown0708
You're comparing apples and oranges. The spaces @eremite talks about are those *within* curly, not those on the outside. You are talking about leading (and perhaps trailing) spaces. An analogous example of yours would be `[1,2,3].slice( 2 )`
Zano
Hence the word "however"
dbrown0708
+2  A: 

You can use a combination. At a place I used to work, we had a somewhat fuzzy style rule that said "use spaces inside brackets except when they're the outer brackets of a structure." This sounds confusing, but it often helps you arrive at something which makes nice intuitive visual sense.

lambda {(1..5).map {|i| {:a => { :b => i }} }}

I like how this spacing sets off the inner hash (return value), without it feeling too smushed, and the trailing }} helps you see that it's encased by two levels of nesting.

Alison R.
A: 

Up to a point, I think it's a matter of personal taste. Normally I find adding white space helps readability - but not always. In your example, personally, I would:

lambda{ (1..5).map{ |i| {:a=>{:b=>i}} } }

Of course, if you find it problematic to read, you almost always have the option of not using a lambda, if you prefer.

If you're coding with a group of other people -- or for the specific purpose of showing your code to the Ruby community -- you also have to consider what they will find readable.

But I'm not sure I care that strongly whether my example above is "normal ruby spacing".

Shadowfirebird
A: 

The extra space at the start/ends of hashes was probably made good style to improve readability on key/values that start/end with anything but a letter.

{:a => "b"} #is less readable than
{ :a => "b" } #more readable.

It's in part attributed to the curly brackets, I rarely see spaces at the start of hashes in Ruby ([]).
The new hash syntax introduced in Ruby 1.9 shall probably modify this

{a: "b"} #same as {:a => "b"}
Mereghost
A: 

I've always thought it was most readable to include the extra space inside curly brace blocks, but omit the last space if the preceding character is a closing brace. To use your example:

lambda { (1..5).map { |i| { :a => { :b => i }}}}
Vincent