如何在Ruby中注释多行?
答案 0 :(得分:1271)
#!/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.
答案 1 :(得分:119)
=begin
My
multiline
comment
here
=end
答案 2 :(得分:54)
尽管存在=begin
和=end
,但正常且更正确的评论方式是在每一行使用#
。如果您阅读任何ruby库的源代码,您会发现这几乎是所有情况下多线注释的完成方式。
答案 3 :(得分:19)
#!/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!"
答案 4 :(得分:15)
使用:
=begin This is a comment block =end
或
# This # is # a # comment # block
是rdoc当前支持的唯一两个,这是我认为只使用这些的一个很好的理由。
答案 5 :(得分:13)
=begin
(some code here)
=end
和
# This code
# on multiple lines
# is commented out
都是正确的。第一种评论的优点是可编辑性 - 它更容易取消注释,因为删除的字符越少。第二种注释的优点是可读性 - 逐行读取代码,更容易判断特定行已被注释掉。你的电话,但想想谁跟你在一起,以及他们阅读和维护是多么容易。
答案 6 :(得分:12)
以下是一个例子:
=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
答案 7 :(得分:3)
=begin
comment line 1
comment line 2
=end
确保= begin和= end是该行的第一件事(无空格)
答案 8 :(得分:2)
如果有人正在寻找在Ruby on Rails中对html模板中的多行进行注释的方法,那么可能存在= begin = end的问题,例如:
<%
=begin
%>
... multiple HTML lines to comment out
<%= image_tag("image.jpg") %>
<%
=end
%>
由于%&gt; 将失败关闭image_tag。
在这种情况下,也许可以说这是否是评论,但我更喜欢用“if false”块封闭不受欢迎的部分:
<% if false %>
... multiple HTML lines to comment out
<%= image_tag("image.jpg") %>
<% end %>
这样可行。
答案 9 :(得分:1)
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引擎无法正确呈现语法颜色。作为练习,在您选择的编辑器中测试它的呈现方式。 ;)