块注释的精确规范

时间:2012-10-08 12:29:01

标签: ruby block-comments

块注释的确切规范是什么?似乎第一行和最后一行必须与=begin=end完全一致。但除此之外,还有一点不清楚。一些描述说=begin=end必须是相应行中的唯一内容,但这似乎不是真的。运行Ruby 1.9.3 MRI,我得到以下结果。

添加空格字符似乎仍然有效:

=begin \t\t   \t
  This is not a Ruby command and should raise an exception if read.
=end \t\t\t   \t\t\t\t
# => no problem

此外,似乎我可以在一个或多个空格字符后添加任意字符串(不包括“\ n”),它仍然可以:

=begin \t\t   \tblah blah blah
  This is not a Ruby command and should raise an exception if read.
=end \t\t\t   \t\t\t\tThis is some scribble
# => no problem

我可以在块注释的中间放置以=begin开头的行:

=begin
  This is not a Ruby command and should raise an exception if read.
=begin
  So as this one.
=end
# => no problem

但不是一行符合评论的最后一行:

=begin
  This is not a Ruby command and should raise an exception if read.
=end blah blah
  So as this one.
=end
# => error

此规范还是依赖于实现的行为?为清楚起见,有人可以用正则表达式来描述Ruby块注释语法的确切规范吗?

1 个答案:

答案 0 :(得分:2)

Ruby Programming Lanuage,第26页: “Ruby支持另一种称为嵌入式文档的多行注释。(...)

=begin=end之后显示的任何文字都是评论的一部分,也会被忽略,但该额外文字必须与=begin=end分开至少一个空间。(...)

嵌入式文档通常由一些在Ruby源代码上运行的后处理工具提供,并且通常使用=begin跟随标识符指示注释所针对的工具。“

另一种使用方式:

=begin Please fix this!
non working code #this is a comment line
=end non working code

#=begin Please fix this!
non working code #now this line gets run
#=end non working code
相关问题