使用花括号是否违反“Ruby 方式”?
我已经使用 Ruby 大约两周了,而且我编程的时间并不长,但我是从 C 风格的背景(C++、C# 等)开始接触这门语言的。不管怎样,我的一个好朋友兼导师正在看我前几天写的一些 Ruby,他告诉我,如果他发现我再次在 Ruby 中使用大括号,他会打我。
嗯,我昨天刚刚通过 这篇 About.com 文章,他们发布的示例使用花括号。有没有不同的方法来做到这一点,或者您是否必须在 Builder 中使用花括号?
这看起来似乎是一个小问题,但我是 Ruby 新手,我不想让自己养成任何坏习惯。你们觉得怎么样?
I've been using Ruby for about two weeks, and I've not been programming for too terribly long, but I'm coming at the language from a C-style background (C++, C#, etc). Anyway - a good friend and mentor of mine was looking at some Ruby that I'd written the other day, and he told me that he'd smack me if he caught me using curly braces in Ruby again.
Well, I just found out about Builder yesterday, via this About.com article, and the example that they have posted uses curly braces. Is there a different way to do this, or do you have to use curly braces with Builder?
This may seem like a minor point, but I'm new to Ruby, and I don't want to let myself develop any bad habits. What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然有些人会选择“单行大括号,多行 do-end”,但我个人认为以下规则最合乎逻辑:
do-end
(通常,使用each
和相关方法),并map
、inject
等)此逻辑如下很好地解决了马特写的方法链问题。
这种方法的一个好处是,它会让您在每次编写块时考虑副作用,它们非常重要,尽管有时会被没有函数式编程背景的程序员所忽视。
另一种不涉及副作用术语的说法是:
do-end
{
和}
对于返回的块,以下是几篇包含更多信息的文章:
http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
http://talklikeaduck.denhaven2.com/2007/10/02/ruby-blocks-do-or-brace
While some people go with "braces for one-liners, do-end for multi-liners", I personally find the following rule the most logical:
do-end
when your block has side-effects (typically, witheach
and related methods) andmap
,inject
and alike)This logic goes well with method chaining issue that Matt wrote about.
One benefit of this approach is that it is going to make you think about side-effects every time you write a block, and they are very important, although sometimes overlooked by coders with no functional programming background.
Another way to put it, without involving side-effects terminology would be:
do-end
for blocks that perform{
and}
for blocks that returnHere are couple of articles with more info:
http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc
http://talklikeaduck.denhaven2.com/2007/10/02/ruby-blocks-do-or-brace
惯用的 ruby
原因之一是链接,
foo.map {|f| f.num}.reduce(0) {|备忘录,i| memo + i}
看起来比在end
上挂起一个方法调用更好,就像在
end
上调用一个方法有一些奇怪的地方,即使在语法上两者是等价的。Idiomatic ruby is
One reason for this is for chaining,
foo.map {|f| f.num}.reduce(0) {|memo, i| memo + i}
looks nicer then hanging a method call off of anend
likeThere is just something strange about calling a method off of
end
, even though syntactically the two are equivalent.通常的约定是单行的
{ ... }
块和多行的do ... end
。我通常遵循这个约定,但如果我成为国王,我想我会更频繁地使用do .. end
。{}
偶尔出现的问题是{}
比do end
绑定得更紧密,因此唯一的编写方式对于也有参数的方法,诗歌模式块是使用do end
,否则该块将成为参数的一部分,并且不会直接传递给方法。当然,如果您想要在诗歌模式下将块附加到参数,那么您可以通过
{}
后跟do end
获胜。最后,与您在这里收到的一些建议相反,在
end
之前不需要;
。The usual convention is
{ ... }
blocks for one-liners anddo ... end
for multi-liners. I generally follow this convention but should I ever be king I think I would usedo .. end
more often.An occasional issue with
{}
is that{}
binds more tightly thando end
so the only way to write a poetry-mode block for a method that also has parameters is to usedo end
, otherwise the block will be part of the parameter and will not be not passed directly to the method.Of course, if you wanted to attach a block to a parameter in poetry mode, then you win with
{}
followed bydo end
.Finally, and contrary to some of the advice you have received here, a
;
is not needed beforeend
.“Ruby 方式”并不提倡任何特定的风格。如果他们不希望您可以选择使用大括号,那么语言中就不会出现这种情况。也就是说,符合您团队的编码风格(如果您正在与团队合作)。
我见过的大多数 Ruby 代码都使用
end
代替大括号,但我不使用 Ruby,所以我不能肯定地说这是首选样式。 Ruby 有不止一种做事方式——您可以使用任何您喜欢的方式(在合理范围内)。"The Ruby way" doesn't advocate any specific style. If they didn't want you to have the option to use curly braces, it wouldn't be in the language. That said, conform to your team's coding style (if you're working with a team).
Most Ruby code I've seen uses
end
in place of curly braces, but I don't use Ruby so I can't say for sure that this is the preferred style. Ruby has more than one way of doing things -- you can use whichever way pleases you (within reason).