Ruby 中的多行注释?

发布于 2024-09-04 04:25:25 字数 22 浏览 7 评论 0原文

如何在 Ruby 中注释多行?

How can I comment multiple lines in Ruby?

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

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

发布评论

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

评论(10

┼── 2024-09-11 04:25:25
#!/usr/bin/env ruby

=begin
Every body mentioned this way
to have multiline comments.

The =begin and =end must be at the beginning of the line or
it will be a syntax error.
=end

puts "Hello world!"

<<-DOC
Also, you could create a docstring.
which...
DOC

puts "Hello world!"

"..is kinda ugly and creates
a String instance, but I know one guy
with a Smalltalk background, who
does this."

puts "Hello world!"

##
# most
# people
# do
# this


__END__

But all forgot there is another option.
Only at the end of a file, of course.
  • 这就是它的样子(通过屏幕截图) - 否则很难解释上面的评论会是什么样子。 点击放大

文本编辑器中的注释

#!/usr/bin/env ruby

=begin
Every body mentioned this way
to have multiline comments.

The =begin and =end must be at the beginning of the line or
it will be a syntax error.
=end

puts "Hello world!"

<<-DOC
Also, you could create a docstring.
which...
DOC

puts "Hello world!"

"..is kinda ugly and creates
a String instance, but I know one guy
with a Smalltalk background, who
does this."

puts "Hello world!"

##
# most
# people
# do
# this


__END__

But all forgot there is another option.
Only at the end of a file, of course.
  • This is how it looks (via screenshot) - otherwise it's hard to interpret how the above comments will look. Click to Zoom-in:

Comments in a text-editor

百合的盛世恋 2024-09-11 04:25:25
=begin
My 
multiline
comment
here
=end
=begin
My 
multiline
comment
here
=end
赠我空喜 2024-09-11 04:25:25

尽管存在 =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.

错爱 2024-09-11 04:25:25
#!/usr/bin/env ruby

=begin
Between =begin and =end, any number
of lines may be written. All of these
lines are ignored by the Ruby interpreter.
=end

puts "Hello world!"
#!/usr/bin/env ruby

=begin
Between =begin and =end, any number
of lines may be written. All of these
lines are ignored by the Ruby interpreter.
=end

puts "Hello world!"
久伴你 2024-09-11 04:25:25

使用:

=begin
This
is
a
comment
block
=end

# This
# is
# a
# comment
# block

是 rdoc 当前支持的唯一两个,我认为这是仅使用这些的一个很好的理由。

Using either:

=begin
This
is
a
comment
block
=end

or

# This
# is
# a
# comment
# block

are the only two currently supported by rdoc, which is a good reason to use only these I think.

呆橘 2024-09-11 04:25:25
=begin
comment line 1
comment line 2
=end

确保 =begin=end 是该行的第一件事(没有空格)

=begin
comment line 1
comment line 2
=end

make sure =begin and =end is the first thing on that line (no spaces)

萌辣 2024-09-11 04:25:25

这是一个示例:

=begin 
print "Give me a number:"
number = gets.chomp.to_f

total = number * 10
puts  "The total value is : #{total}"

=end

您放置在 =begin=end 之间的所有内容都将被视为注释,无论其之间包含多少行代码。

注意:确保=begin之间没有空格:

  • 正确:=begin
  • 错误:=开始

Here is an example :

=begin 
print "Give me a number:"
number = gets.chomp.to_f

total = number * 10
puts  "The total value is : #{total}"

=end

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 = and begin:

  • Correct: =begin
  • Wrong: = begin
迷离° 2024-09-11 04:25:25
=begin
(some code here)
=end

并且

# This code
# on multiple lines
# is commented out

都是正确的。第一种注释类型的优点是可编辑性 - 更容易取消注释,因为删除的字符较少。第二种注释的优点是可读性——逐行阅读代码,更容易看出特定行已被注释掉。你的电话,但想想谁会追随你,以及他们阅读和维护是多么容易。

=begin
(some code here)
=end

and

# This code
# on multiple lines
# is commented out

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.

寂寞美少年 2024-09-11 04:25:25

如果有人正在寻找一种注释多行的替代方法,例如:

<%
=begin
%>
  ... multiple HTML lines to comment out
  <%= image_tag("image.jpg") %>
<%
=end
%>

可以替换为:

<% if false # The following section is commented out because... %>
  ... multiple HTML lines to comment out
  <%= image_tag("image.jpg") %>
<% end # if false %>

这不是严格的注释,因为有一个 if 语句,因此对 CPU 的使用很小,所以我不会不要在频繁运行的代码中使用此类资源。

但有时我发现它更有吸引力。

有谁知道这种做法的主内存使用是否有差异?

In case someone is looking for an alternate way to comment multiple lines, for instance:

<%
=begin
%>
  ... multiple HTML lines to comment out
  <%= image_tag("image.jpg") %>
<%
=end
%>

can be replaced with:

<% if false # The following section is commented out because... %>
  ... multiple HTML lines to comment out
  <%= image_tag("image.jpg") %>
<% end # if false %>

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?

吖咩 2024-09-11 04:25:25
  def idle
    <<~aid
    This is some description of what idle does.

    It does nothing actually, it's just here to show an example of multiline
    documentation. Thus said, this is something that is more common in the
    python community. That's an important point as it's good to also fit the
    expectation of your community of work. Now, if you agree with your team to
    go with a solution like this one for documenting your own base code, that's
    fine: just discuss about it with them first.

    Depending on your editor configuration, it won't be colored like a comment,
    like those starting with a "#". But as any keyword can be used for wrapping
    an heredoc, it is easy to spot anyway. One could even come with separated
    words for different puposes, so selective extraction for different types of
    documentation generation would be more practical. Depending on your editor,
    you possibly could configure it to use the same syntax highlight used for
    monoline comment when the keyword is one like aid or whatever you like.

    Also note that the squiggly-heredoc, using "~", allow to position
    the closing term with a level of indentation. That avoids to break the visual reading flow, unlike this far too long line.
    aid
  end

请注意,在发布本文时,stackoverflow 引擎无法正确渲染语法着色。测试它在您选择的编辑器中的呈现方式可以作为练习。 ;)

  def idle
    <<~aid
    This is some description of what idle does.

    It does nothing actually, it's just here to show an example of multiline
    documentation. Thus said, this is something that is more common in the
    python community. That's an important point as it's good to also fit the
    expectation of your community of work. Now, if you agree with your team to
    go with a solution like this one for documenting your own base code, that's
    fine: just discuss about it with them first.

    Depending on your editor configuration, it won't be colored like a comment,
    like those starting with a "#". But as any keyword can be used for wrapping
    an heredoc, it is easy to spot anyway. One could even come with separated
    words for different puposes, so selective extraction for different types of
    documentation generation would be more practical. Depending on your editor,
    you possibly could configure it to use the same syntax highlight used for
    monoline comment when the keyword is one like aid or whatever you like.

    Also note that the squiggly-heredoc, using "~", allow to position
    the closing term with a level of indentation. That avoids to break the visual reading flow, unlike this far too long line.
    aid
  end

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

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