Ruby中的多行注释?

如何在Ruby中注释多行?

488466 次浏览
=beginMymultilinecommenthere=end
#!/usr/bin/env ruby
=beginBetween =begin and =end, any numberof lines may be written. All of theselines are ignored by the Ruby interpreter.=end
puts "Hello world!"
#!/usr/bin/env ruby
=beginEvery body mentioned this wayto have multiline comments.
The =begin and =end must be at the beginning of the line orit will be a syntax error.=end
puts "Hello world!"
<<-DOCAlso, you could create a docstring.which...DOC
puts "Hello world!"
"..is kinda ugly and createsa String instance, but I know one guywith a Smalltalk background, whodoes 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,但更正常和更正确的注释方式是在每一行上使用#。如果您阅读任何ruby库的源代码,您会发现这是几乎所有情况下多行注释的方式。

=begin(some code here)=end

# This code# on multiple lines# is commented out

两种注释都是正确的。第一种注释的优点是可编辑性——因为删除的字符更少,所以更容易取消注释。第二种注释的优点是易读性——逐行阅读代码,更容易看出某一行被注释了。你的电话,但想想谁在跟踪你,他们阅读和维护有多容易。

使用任一:

=beginThisisacommentblock=end

# This# is# a# comment# block

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

下面是一个例子:

=beginprint "Give me a number:"number = gets.chomp.to_f
total = number * 10puts  "The total value is : #{total}"
=end

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

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

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

如果有人在Ruby on Rails中寻找一种在html模板中注释多行的方法,则可能存在=开始=结束的问题,例如:

<%=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 %>

这将工作。

=begincomment line 1comment line 2=end

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

  def idle<<~aidThis is some description of what idle does.
It does nothing actually, it's just here to show an example of multilinedocumentation. Thus said, this is something that is more common in thepython community. That's an important point as it's good to also fit theexpectation of your community of work. Now, if you agree with your team togo with a solution like this one for documenting your own base code, that'sfine: 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 wrappingan heredoc, it is easy to spot anyway. One could even come with separatedwords for different puposes, so selective extraction for different types ofdocumentation generation would be more practical. Depending on your editor,you possibly could configure it to use the same syntax highlight used formonoline comment when the keyword is one like aid or whatever you like.
Also note that the squiggly-heredoc, using "~", allow to positionthe closing term with a level of indentation. That avoids to break the visual reading flow, unlike this far too long line.aidend

请注意,在文章的那一刻,stackoverflow引擎没有正确呈现语法着色。测试它在您选择的编辑器中的呈现方式作为练习。;)