Answers:
#!/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.
#
全部使用它们,主要是因为它在视觉上比=begin
/ =end
或使用here-to方法更好地分隔了注释行。而且,很好。
=begin
并且=end
不能在其前面加上任何空格。
=begin...=end
使用的第一个和最后一个块#
。
=begin
My
multiline
comment
here
=end
#
在每行之前输入a 和空格?击键很多,尤其是当我开始添加换行符时。
尽管存在=begin
和=end
,但通常的更正确的注释方法是#
在每行上使用。如果您阅读任何ruby库的源代码,您将发现这几乎是在所有情况下进行多行注释的方式。
#
因为它更明显。在注释掉代码时,很重要的一点就是要弄清楚发生了什么。如果您在查看代码时没有在编辑器中使用代码着色的好处,则使用=begin/=end
可能很难弄清为什么忽略代码。
#
注释。(我很奇怪,为什么会有两次失败。我想有时候Stack Overflow社区必须弄错了!)
3 == three
在哪里def three; 1 + 1 + 1 end
。因此,两者都是有效的。谁在乎?使用3
!
vi
生产服务器上使用时。在这种情况下,无论如何,您可能不应该在那里进行开发。
#!/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!"
/*I am a\n#nested\ncomment, which really serves no purpose*/
/*I am bound /*to*/ FAIL!*/
如果您在多行注释中包含单行注释和代码,例如具有您不希望人们使用的带有文档的功能,但又不想从文件中删除它,这可能很有意义。
=begin
(some code here)
=end
和
# This code
# on multiple lines
# is commented out
都是正确的。第一种类型的注释的优点是可编辑性,因为删除了较少的字符,因此更易于取消注释。第二种类型的注释的优点是可读性-逐行读取代码,很容易分辨出特定的行已被注释掉。您打来的电话,但请考虑谁在追随您,以及他们阅读和维护它们有多容易。
=begin
而=end
不会在视觉上传达的是在两者之间是一条评论... Clojure的,例如,使用(comment :whatever)
其中的线索说,这意味着什么:stackoverflow.com/questions/1191628/block-comments-in-clojure
这是一个例子:
=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
如果有人在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 %>
这将起作用。
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引擎无法正确呈现语法颜色。作为练习,在您选择的编辑器中测试它的呈现方式。;)
.pp
清单中寻找多行注释(基于类似Ruby的语法),您可以使用c样式的块注释/**/