使用花括号是否违反“Ruby 方式”?

发布于 2024-10-20 03:03:41 字数 350 浏览 9 评论 0原文

我已经使用 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 技术交流群。

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

发布评论

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

评论(4

请帮我爱他 2024-10-27 03:03:41

虽然有些人会选择“单行大括号,多行 do-end”,但我个人认为以下规则最合乎逻辑:

  • 当你的块有副作用时使用 do-end (通常,使用 each 和相关方法),并
  • 在块没有副作用时使用大括号(mapinject 等)

此逻辑如下很好地解决了马特写的方法链问题。

这种方法的一个好处是,它会让您在每次编写块时考虑副作用,它们非常重要,尽管有时会被没有函数式编程背景的程序员所忽视。

另一种不涉及副作用术语的说法是:

  • 执行的块使用 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:

  • use do-end when your block has side-effects (typically, with each and related methods) and
  • use braces when your block is without side-effects (map, 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:

  • use do-end for blocks that perform
  • use { and } for blocks that return

Here 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

夜空下最亮的亮点 2024-10-27 03:03:41

惯用的 ruby

method_name {|param| param.do_something} # squigglies for one liners

# do/end for multi-line
method_name do |param|
  param.do_something
end

​​ 原因之一是链接,foo.map {|f| f.num}.reduce(0) {|备忘录,i| memo + i} 看起来比在 end 上挂起一个方法调用更好,就像

foo.map do |f| 
  f.num
end.reduce(0) do |memo, i| 
  memo + i
end

end 上调用一个方法有一些奇怪的地方,即使在语法上两者是等价的。

Idiomatic ruby is

method_name {|param| param.do_something} # squigglies for one liners

# do/end for multi-line
method_name do |param|
  param.do_something
end

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 an end like

foo.map do |f| 
  f.num
end.reduce(0) do |memo, i| 
  memo + i
end

There is just something strange about calling a method off of end, even though syntactically the two are equivalent.

二智少女猫性小仙女 2024-10-27 03:03:41

通常的约定是单行的 { ... } 块和多行的 do ... end 。我通常遵循这个约定,但如果我成为国王,我想我会更频繁地使用 do .. end

{} 偶尔出现的问题是 {}do end 绑定得更紧密,因此唯一的编写方式对于也有参数的方法,诗歌模式块是使用 do end,否则该块将成为参数的一部分,并且不会直接传递给方法。

def f x
  yield x
end

f 123 do |n| p n end  # works
f 123  { |n| p n }    # does not work
f(123) { |n| p n }    # works, of course

当然,如果您想要在诗歌模式下将块附加到参数,那么您可以通过 {} 后跟 do end 获胜。

def g  ; p ['g', yield] end
def f x; p ['f', yield] end

f g { 2 } do 3 end
["g", 2]
["f", 3]

最后,与您在这里收到的一些建议相反,在 end 之前不需要;

The usual convention is { ... } blocks for one-liners and do ... end for multi-liners. I generally follow this convention but should I ever be king I think I would use do .. end more often.

An occasional issue with {} is that {} binds more tightly than do end so the only way to write a poetry-mode block for a method that also has parameters is to use do end, otherwise the block will be part of the parameter and will not be not passed directly to the method.

def f x
  yield x
end

f 123 do |n| p n end  # works
f 123  { |n| p n }    # does not work
f(123) { |n| p n }    # works, of course

Of course, if you wanted to attach a block to a parameter in poetry mode, then you win with {} followed by do end.

def g  ; p ['g', yield] end
def f x; p ['f', yield] end

f g { 2 } do 3 end
["g", 2]
["f", 3]

Finally, and contrary to some of the advice you have received here, a ; is not needed before end.

下雨或天晴 2024-10-27 03:03:41

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

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