如何在Ruby中注释多行?
当前回答
如果有人在RubyonRails中寻找在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 %>
这会奏效的。
其他回答
如果有人在RubyonRails中寻找在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和=end,但正常且更正确的注释方式是在每行上使用#。如果您阅读任何ruby库的源代码,您会发现几乎在所有情况下都是这样进行多行注释的。
#!/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
(some code here)
=end
and
# This code
# on multiple lines
# is commented out
都是正确的。第一种注释类型的优点是可编辑性,因为删除的字符更少,所以取消注释更容易。第二类注释的优点是逐行读取代码的可读性,更容易判断某一行已被注释掉。你的电话,但想想谁会来找你,他们阅读和维护起来有多容易。
=begin
comment line 1
comment line 2
=end
确保=beginand=end是该行的第一项(没有空格)
推荐文章
- 如何记录Ruby代码?
- C多行宏:do/while(0) vs作用域块
- Ruby:包含的反义词是什么?Ruby数组?
- 在Eclipse中粘贴多行Java字符串
- 在Ruby中,proc和lambda有什么区别?
- 如何传递参数到一个Rake任务与环境在Rails?
- 获取当前正在执行的方法的名称
- 如何在Rails中计算相对时间?
- 有没有办法注释掉.ASPX页面中的标记?
- 如何在Python自己的调试器(PDB)中执行多行语句
- 在Ruby中使用范围填充数组的正确方法
- “for”和“each”在Ruby中
- 我如何复制一个哈希在Ruby?
- Ruby/Rails:将Date转换为UNIX时间戳
- 我如何编码/解码HTML实体在Ruby?