2

ブロックコメントの正確な仕様は何ですか?最初と最後の行は正確にとで始まらなければならないのは本当のよう=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

さらに、1つ以上のスペース文字の後に任意の文字列(「\ 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ブロックコメント構文の正確な仕様を説明できますか?

4

1 に答える 1

3

Rubyプログラミング言語、26ページ:「Rubyは、埋め込みドキュメントと呼ばれる別のスタイルの複数行コメントをサポートしています。(...)

コメントの後に表示される、=beginまたは=endコメントの一部であり、無視されるテキスト。ただし、その余分なテキストは、=beginおよびから=end少なくとも1つのスペースで区切る必要があります。(...)

埋め込まれたドキュメントは通常、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
于 2012-10-08T13:02:44.657 に答える