Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby whitespace: Is { :a => 1 } better than {:a => 1}? [closed]

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?

like image 443
eremite Avatar asked Feb 10 '10 17:02

eremite


2 Answers

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

like image 164
Pedro Rolo Avatar answered Oct 24 '22 18:10

Pedro Rolo


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

like image 27
David J. Avatar answered Oct 24 '22 19:10

David J.