Ruby的样式显示长数组 [英] Ruby style for displaying long arrays

查看:93
本文介绍了Ruby的样式显示长数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 rubocop 来检查我的code的造型惯例符合最佳实践。我有一个颜色数组,想知道来显示它们的最佳方式。我读过行应少于80个字符长,所以我这样做。

I am using rubocop to check the styling conventions of my code conform to best practices. I have an array of colors and want to know the best way to display them. I've read that lines should be less than 80 characters long, so I did this.

colors = [:light_red, :red, :pink, :orange, :light_yellow, :yellow,
          :light_green, :green, :light_blue, :blue, :white, :black]

我从rubocop以下异常消息

I get the following exception message from rubocop

C: Align the elements of an array literal if they span more than one line

这是否意味着,我应该做这样的事情。

Does this mean, I should have done something like this

colors = [:light_red,
          :red,
          :pink,
          :orange,
          :light_yellow,
          :yellow,
          :light_green,
          :green,
          :light_blue
          :blue,
          :white,
          :black]

这会占用非常多的空间,我想我的方法是更有效的。

That takes up an awful lot of space, and I thought my way was more efficient.

什么是关于在Ruby中显示多行阵列的文体规则?

What is the stylistic rule regarding displaying multiline arrays in ruby?

推荐答案

有什么地方行尾不同的想法,所以我没有提及。

There are different thoughts about where to end a line, so I do not mention that.

如果你要改变的项目之间的界线在列表中,有一件事我能说的是,你应该这样做很好的分隔符之间( [] 在这种情况下)和第一/最后一个项目。不是:

In case you are going to change lines between the items in a list, one thing I can say is that, you should do so as well between the delimiter characters ([ and ] in this case) and the first/last item. Not:

....... [:foo,
         :bar,
         :baz]

....... [
          :foo,
          :bar,
          :baz,
        ]

这是缩进相对分隔符的缩进级别两个空格的项目的约定。

It is a convention to indent the items two spaces relative to the indentation level of the delimiters.

另一个技巧是最后一个项目之后加一个逗号如上图所示。虽然这是可选的,并且具有对code没有影响,这将使它更容易为你以后编辑列表(更改顺序,添加,删除的项目,等等)。

Another tip is to add a comma after the last item as shown above. Although this is optional, and has no effect to the code, this will make it easier for you to later edit the list (change the order, add, delete the items, etc.).

这篇关于Ruby的样式显示长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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