【发布时间】:2011-02-28 16:56:34
【问题描述】:
如何在 Ruby 中注释多行?
【问题讨论】:
-
不幸的是,ruby 中的多行 cmets 看起来非常像代码块。考虑到这个问题的高分(以及公认的答案),研究 ruby 语法的人应该清楚地考虑一下。
标签: ruby comments multiline block-comments
如何在 Ruby 中注释多行?
【问题讨论】:
标签: ruby comments multiline block-comments
=begin
My
multiline
comment
here
=end
【讨论】:
# 和空格?这是很多击键,尤其是当我开始添加换行符时。
#!/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!*/ 如果您在多行注释中有单行 cmets 和代码,例如带有文档的函数,您不希望人们使用,但您也不想删除它,这可能是有意义的从文件中。
#!/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 和最后一个使用 # 的块在生成文档时被 rdoc 拾取。
尽管存在=begin 和=end,但正常且更正确的注释方式是在每一行使用#。如果您阅读任何 ruby 库的源代码,您会发现在几乎所有情况下,多行 cmets 都是采用这种方式。
【讨论】:
#,因为它更明显。在注释掉代码时,重要的是要明确这就是发生的事情。如果您在使用 =begin/=end 的编辑器中查看代码时没有使用代码着色的好处,可能很难弄清楚代码被忽略的原因。
# cmets。 (我很困惑为什么这有两个反对意见。我猜 Stack Overflow 社区有时会弄错!)
3 == three 其中def three; 1 + 1 + 1 end。因此两者都是有效的。谁在乎?使用3!
vi 时。在这种情况下,无论如何,您可能不应该在那里进行开发。
=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
使用任一:
=开始 这 是 一种 评论 堵塞 =结束或
# 这 # 是 # 一种 # 评论 # 堵塞是 rdoc 目前支持的仅有的两个,我认为这是只使用这些的一个很好的理由。
【讨论】:
=begin 或# 的另一个充分理由是<<-DOC 和" 语法都会在执行时生成无用的字符串文字。
这是一个例子:
=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 %>
这会起作用。
【讨论】:
=begin
comment line 1
comment line 2
=end
确保=begin 和=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 引擎无法正确呈现语法着色。测试它在您选择的编辑器中的呈现方式是一项练习。 ;)
【讨论】: