2012-10-08 17 views
1

塊註釋的確切規格是什麼?似乎確實,第一行和最後一行必須以和=end開頭。但除此之外,還有一點不明朗之處。一些描述說,和=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 

Furturemore,看來我可以添加任意字符串(不包括「\ 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塊註釋語法的確切規範嗎?

回答

2

Ruby編程Lanuage,第26頁: 「Ruby支持被稱爲嵌入文檔多行註釋的另一種風格(...)

出現後或=end是部分的任何文字。 (...)

嵌入式文檔通常是由一些後處理工具打算的,該工具運行在Ruby源代碼中,並且這些文檔可能會被忽略,但是額外的文本必須與和=end至少分隔一個空格。 ,並且通常按照和ide指明評論意圖爲哪個工具。「

使用的另一種方式:

=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 
+0

其實,這仍不能準確地一樣我得到的結果。評論似乎能夠在沒有空間的情況下遵循標籤序列。 – sawa