Ruby空格:是{:a => 1}比{:a => 1}? [英] Ruby whitespace: Is { :a => 1 } better than {:a => 1}?

查看:160
本文介绍了Ruby空格:是{:a => 1}比{:a => 1}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看其他人的代码,看起来很常见的是在大括号内包含一个额外的空间。这是有原因吗?对我来说,它似乎添加额外的按键为增加的丑陋。特别是当事物嵌套:

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

由于某些原因,它看起来更简洁一致:

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

也许额外的空格是一些文本编辑器的副作用或者是历史原因或者其他的东西?我没有看到这样的风格指南,如果这就像2空间缩进我想按照惯例,但如果没有很好的理由,我想我会继续做我自己的方式。你喜欢哪一种,为什么?

解决方案

我看到的大部分Ruby代码(希望所有代码都是我写的)这种风格:

  {:key => 'value'} 

这就是我已经习惯和内化的内容。
$ b

当一切都说完之后,这个特定的风格问题并不是最重要的。也就是说,Ruby社区(与其他人一样)认为,将您的风格(a)与您正在使用的项目以及(b)与整个代码社区相匹配非常重要。这就是为什么我建议使用额外的空格。



顺便说一句,这是一个很好的Ruby风格指南:
http://www.caliban.org/ruby/rubyguide.shtml#style


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?

解决方案

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

这篇关于Ruby空格:是{:a => 1}比{:a => 1}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆