Ruby 中的多行注释?
如何在 Ruby 中注释多行?
How can I comment multiple lines in Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 Ruby 中注释多行?
How can I comment multiple lines in Ruby?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
尽管存在
=begin
和=end
,但正常且更正确的注释方法是在每一行上使用#
。如果您阅读任何 ruby 库的源代码,您会发现这就是几乎所有情况下多行注释的完成方式。Despite the existence of
=begin
and=end
, the normal and a more correct way to comment is to use#
's on each line. If you read the source of any ruby library, you will see that this is the way multi-line comments are done in almost all cases.使用:
或
是 rdoc 当前支持的唯一两个,我认为这是仅使用这些的一个很好的理由。
Using either:
or
are the only two currently supported by rdoc, which is a good reason to use only these I think.
确保
=begin
和=end
是该行的第一件事(没有空格)make sure
=begin
and=end
is the first thing on that line (no spaces)这是一个示例:
您放置在
=begin
和=end
之间的所有内容都将被视为注释,无论其之间包含多少行代码。注意:确保
=
和begin
之间没有空格:=begin
=开始
Here is an example :
Everything you place in between
=begin
and=end
will be treated as a comment regardless of how many lines of code it contains between.Note: Make sure there is no space between
=
andbegin
:=begin
= begin
并且
都是正确的。第一种注释类型的优点是可编辑性 - 更容易取消注释,因为删除的字符较少。第二种注释的优点是可读性——逐行阅读代码,更容易看出特定行已被注释掉。你的电话,但想想谁会追随你,以及他们阅读和维护是多么容易。
and
are both correct. The advantage of the first type of comment is editability—it's easier to uncomment because fewer characters are deleted. The advantage of the second type of comment is readability—reading the code line by line, it's much easier to tell that a particular line has been commented out. Your call but think about who's coming after you and how easy it is for them to read and maintain.
如果有人正在寻找一种注释多行的替代方法,例如:
可以替换为:
这不是严格的注释,因为有一个 if 语句,因此对 CPU 的使用很小,所以我不会不要在频繁运行的代码中使用此类资源。
但有时我发现它更有吸引力。
有谁知道这种做法的主内存使用是否有差异?
In case someone is looking for an alternate way to comment multiple lines, for instance:
can be replaced with:
This is not strictly commenting, because there is an if sentence, and therefore there is a very small use of the CPU, so I wouldn't use this kind of resource inside code that runs frequently.
However sometimes I find it more appealing.
Does anyone know if there is a difference in main memory usage with this practice?
请注意,在发布本文时,stackoverflow 引擎无法正确渲染语法着色。测试它在您选择的编辑器中的呈现方式可以作为练习。 ;)
Note that at the moment of the post, the stackoverflow engine doesn't render syntax coloration correctly. Testing how it renders in your editor of choice is let as an exercise. ;)