【问题标题】:Exact specification for block comment块注释的确切规范
【发布时间】:2012-09-28 16:57:56
【问题描述】:

block cmets 的具体规格是什么?第一行和最后一行必须完全以=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 块注释语法的确切规范吗?

【问题讨论】:

    标签: ruby comments block-comments


    【解决方案1】:

    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
    

    【讨论】:

    • 实际上,这仍然与我得到的结果不完全相同。该评论似乎能够跟随一系列没有空格的标签。
    猜你喜欢
    • 2021-04-14
    • 2014-04-08
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 2014-02-17
    • 1970-01-01
    • 2013-08-01
    相关资源
    最近更新 更多