Ruby whitespace: Is { :a => 1 } better than {:a =>

2019-06-16 03:53发布

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?

9条回答
劳资没心,怎么记你
2楼-- · 2019-06-16 04:30

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

查看更多
SAY GOODBYE
3楼-- · 2019-06-16 04:33

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.

查看更多
爷、活的狠高调
4楼-- · 2019-06-16 04:34

IMHO, in most cases spaces behave as syntatic noise and are annoying to type... I find it surprising the latest obsession of the ruby community with them, with the few ones that care about them trying to impose them as the norm after 10 years with no such norm at all....

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-06-16 04:35

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.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-06-16 04:35

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 }}}}
查看更多
可以哭但决不认输i
7楼-- · 2019-06-16 04:38

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.

查看更多
登录 后发表回答