Ruby 块注释的精确规范
区块注释的确切规格是什么?似乎第一行和最后一行必须以Ruby 块注释的精确规范,ruby,block-comments,Ruby,Block Comments,区块注释的确切规格是什么?似乎第一行和最后一行必须以=begin和=end开始。但除此之外,还有一点不公平。一些描述说,=begin和=end必须是相应行中的唯一内容,但这似乎不是真的。运行Ruby1.9.3MRI,我得到以下结果 添加空白字符似乎仍然有效: =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 p
=begin
和=end
开始。但除此之外,还有一点不公平。一些描述说,=begin
和=end
必须是相应行中的唯一内容,但这似乎不是真的。运行Ruby1.9.3MRI,我得到以下结果
添加空白字符似乎仍然有效:
=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
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块注释语法的确切规范吗?Ruby编程语言,第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
事实上,这与我得到的结果仍然不完全相同。该评论似乎能够在没有空格的情况下跟随一系列标签。