814

如何在 Ruby 中评论多行?

4

10 回答 10

1446
#!/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.
  • 这就是它的外观(通过屏幕截图) - 否则很难解释上述评论的外观。点击放大

文本编辑器中的注释

于 2010-06-07T16:25:40.457 回答
136
=begin
My 
multiline
comment
here
=end
于 2010-06-07T13:12:31.873 回答
62

尽管存在=beginand =end,但正常且更正确的注释方式是#在每一行使用 's。如果您阅读任何 ruby​​ 库的源代码,您会发现这几乎是所有情况下的多行注释方式。

于 2010-06-08T18:24:08.007 回答
22
#!/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!"
于 2010-06-07T13:12:57.183 回答
18

使用任一:

=开始
这个
是
一个
评论
堵塞
=结束

或者

# 这个
# 是
# 一个
# 评论
# 堵塞

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

于 2014-08-05T19:52:21.447 回答
15
=begin
comment line 1
comment line 2
=end

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

于 2018-08-28T15:03:34.197 回答
14
=begin
(some code here)
=end

# This code
# on multiple lines
# is commented out

都是正确的。第一种注释的优点是可编辑性——取消注释更容易,因为删除的字符更少。第二种注释的优点是可读性——逐行阅读代码,更容易判断某行已被注释掉。您的电话,但请考虑谁会追随您,以及他们阅读和维护的难易程度。

于 2014-04-21T03:08:18.473 回答
14

这是一个例子:

=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
  • 错误的:= begin
于 2014-08-15T16:02:29.627 回答
3

如果有人正在寻找一种在 Ruby on Rails 的 html 模板中注释多行的方法,那么 =begin =end 可能会出现问题,例如:

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

由于 %> 关闭 image_tag 将失败。

在这种情况下,这是否是注释掉可能是有争议的,但我更喜欢用“if false”块括起不需要的部分:

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

这将起作用。

于 2018-06-17T21:59:29.217 回答
2
  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 引擎无法正确呈现语法着色。测试它在您选择的编辑器中的呈现方式是一项练习。;)

于 2020-04-26T03:57:30.440 回答