2

What is the exact specification for block comments? It seems to be true that the first and the last lines have to start exactly with =begin and =end. But besides that, there is a little unclarity. Some descriptions say =begin and =end must be the only thing on the respective lines, but that does not seem to be true. Running Ruby 1.9.3 MRI, I get the following results.

Adding white space characters still seems to work:

=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, it seems that I can add an arbitrary string (not including "\n") after one or more space characters, and it is still okay:

=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

I can put a line starting with =begin in the middle of a block comment:

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

But not a line that qualifies as the last line of the comment:

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

Is this specification, or an implementation-dependent behavior? For clarity, can someone describe in terms of regex the exact specification of a Ruby block comment syntax?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
sawa
  • 165,429
  • 45
  • 277
  • 381

1 Answers1

3

The Ruby Programming Lanuage, page 26: "Ruby supports another style of multiline comment known as an embedded document.(...)

Any text that appears after =begin or =end is part of the comment and is also ignored, but that extra text must be separated from the =begin and =end by at least one space.(...)

Embedded documents are usually intended by some postprocessing tool that is run over the Ruby source code, and it's typical to follow =begin with an identifier that indicates which tool the comment is intended for."

Another way of use:

=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
steenslag
  • 79,051
  • 16
  • 138
  • 171
  • Actually, that is still not accurately the same as the result I got. The comment seems to be able to follow a seqeunce of tabs without a space. – sawa Oct 08 '12 at 16:20