什么是“Ruby 方式”?对齐变量赋值? (代码风格问题)

发布于 2024-12-07 00:19:01 字数 615 浏览 1 评论 0原文

假设我有这段未受污染的代码:

some_var = Hash.new
some_var[:name] = "blah"
some_var[:address] = "101 blahblah lane"

another = "Olly olly oxen free!"

Ruby 让它变得漂亮的方法是什么?

选项 1:保持原样

选项 2:对齐相关内容

some_var           = Hash.new
some_var[:name]    = "blah"
some_var[:address] = "101 blahblah lane"

another = "Olly olly oxen free!"

选项 3:对齐所有内容

some_var           = Hash.new
some_var[:name]    = "blah"
some_var[:address] = "101 blahblah lane"

another            = "Olly olly oxen free!"

选项 4:??????

谢谢!!

Say i have this untainted code:

some_var = Hash.new
some_var[:name] = "blah"
some_var[:address] = "101 blahblah lane"

another = "Olly olly oxen free!"

What is the ruby way of making it pretty?

option 1: Leave it as is

option 2: Align related things

some_var           = Hash.new
some_var[:name]    = "blah"
some_var[:address] = "101 blahblah lane"

another = "Olly olly oxen free!"

option 3: Align all the things

some_var           = Hash.new
some_var[:name]    = "blah"
some_var[:address] = "101 blahblah lane"

another            = "Olly olly oxen free!"

option 4: ?????

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

闻呓 2024-12-14 00:19:01

在你的例子中使用哈希我会做另一个版本:

some_var = {
  :name    => "blah",
  :address => "101 blahblah lane",
}
another = "Olly olly oxen free!"

或者使用 Ruby 1.9

some_var = {
  name:    "blah",
  address: "101 blahblah lane",
}
another = "Olly olly oxen free!"

Using Hashes as in your example I would do another version:

some_var = {
  :name    => "blah",
  :address => "101 blahblah lane",
}
another = "Olly olly oxen free!"

or with Ruby 1.9

some_var = {
  name:    "blah",
  address: "101 blahblah lane",
}
another = "Olly olly oxen free!"
坏尐絯℡ 2024-12-14 00:19:01

我真的不喜欢#2 或#3。当你排列这样的东西时,你就将它们组合在一起。即使它们是“对称的”,这也完全不是您在变量赋值中想要的。就像

a      = 1
foobar = 2
baz    = 3

视觉上一样,您将 a、foobar 和 baz 分组,并将 1、2 和 3 分组。即使 1、2 和 3 相关,它们仍然应该与其标签分组,显示它们相关可以通过以下方式完成作业周围的有效空白。

现在,数据结构是你确实想要分组的东西的一个例子

some_method([[1, "foo"],
  [2, "bar"],
  [3, "baz"]])

,在我看来,它比这更难阅读,

some_method([[1, "foo"],
             [2, "bar"],
             [3, "baz"]])

这是一种情况,但我有一个#4给你。

some_var = Hash.new.tap do |h|
  h[:name] = "blah"
  h[:address] = "101 blahblah lane"
end

another = "Olly olly oxen free!"

Object#tap 将产生您点击到块中的东西,并将该块评估为您点击的东西。因此,在该示例中,Hash.new 被生成,被块修改,然后返回并分配给 some_var。与我一起工作的一些人绝对讨厌它,但我发现它在说“这里的所有内容都与初始化这个表达式直接相关”方面做得非常出色

I really don't like #2 or #3. When you align stuff like that, you are grouping things together. Even if they are "symmetrical", that is totally not what you want in variable assignment. like

a      = 1
foobar = 2
baz    = 3

visually, you are grouping a, foobar, and baz, and grouping 1, 2, and 3. Even if 1, 2, and 3 are related, they should still be grouped with their labels, showing they are related can be accomplished by effective whitespace around the assignments.

Now, datastructures are an example of something you DO want to group

some_method([[1, "foo"],
  [2, "bar"],
  [3, "baz"]])

is (imo) way harder to read then

some_method([[1, "foo"],
             [2, "bar"],
             [3, "baz"]])

This is sort of situational, but I have a #4 for you.

some_var = Hash.new.tap do |h|
  h[:name] = "blah"
  h[:address] = "101 blahblah lane"
end

another = "Olly olly oxen free!"

Object#tap will yield the thing you tap into the block, and evaluate the block to be the thing that you tapped. So in that example, Hash.new gets yielded in, modified by the block, and then returned to be assigned to some_var. Some people I work with absolutely hates it, but I find it does a fantastic job of saying "Everything in here is directly related to initializing this expression"

提笔书几行 2024-12-14 00:19:01

恕我直言,1 是最常见的一种,但我肯定见过 2。另外,至少在一份风格指南中也提到了后一种:

当多个对称分配相继出现在一行上时,
对齐它们的赋值运算符。另一方面,不对齐
不相关的赋值的赋值运算符看起来很相似
纯属巧合。

理由:对齐可以帮助读者认识到作业是
相关或对称。

http://en.opensuse.org/openSUSE:Ruby_coding_conventions#Assignment

IMHO 1 would be the most common one, but I've definitely seen 2. Plus the latter one is also mentioned in at least one style guide:

When multiple symmetrical assignments are on lines one after another,
align their assignment operators. On the other hand, do not align
assignment operators of unrelated assignments which look similar just
by coincidence.

Rationale: Alignment helps reader recognize that the assignments are
related or symmetrical.

http://en.opensuse.org/openSUSE:Ruby_coding_conventions#Assignment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文