Exact specification for block comment - ruby

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?

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

Related

Nested multiline comments in ruby?

I'm fairly new to ruby. I've noticed that multiline comments are not nestable. This code causes an error:
=begin
=begin
=end
=end
this is the error:
SyntaxError: (irb):4: syntax error, unexpected '='
from C:/Ruby/bin/irb.cmd:19:in `<main>'
Is there a way to nest comments in ruby?
I don't believe there is. The problem ruby sees here is like this:
=begin # start comment
=begin # still commenting
=end # end commenting
=end # wtf is this?

Ruby: Why do I get warning "regex literal in condition" here?

A simple Ruby program, which works well (using Ruby 2.0.0):
#!/usr/bin/ruby
while gets
print if /foo/../bar/
end
However, Ruby also outputs the warning warning: regex literal in condition. It seems that Ruby considers my flip-flop-expression /foo/../bar/ as dangerous.
My question: Where lies the danger in this program? And: Can I turn off this warning (ideally only for this statement, keeping other warnings active)?
BTW, I found on the net several discussions of this kind of code, also mentioning the warning, but never found a good explanation why we get warned.
You can avoid the warning by using an explicit match:
while gets
print if ~/foo/..~/bar/
end
Regexp#~ matches against $_.
I don't know why the warning is shown (to be honest, I wasn't even aware that Ruby matches regexp literals against $_ implicitly), but according to the parser's source code, it is shown unless you provide the -e option when invoking Ruby, i.e. passing the script as an argument:
$ ruby -e "while gets; print if /foo/../bar/ end"
I would avoid using $_ as an implicit parameter and instead use something like:
while line = gets
print line if line=~/foo/..line=~/bar/
end
I think Neil Slater is right: It looks like a bug in a parser. If I change the code to
#!/usr/bin/ruby
while gets
print if $_=~/foo/..$_=~/bar/
end
the warning disappears.
I'll file a bug report.

How to add line break in rdoc

I have a code and comments for RDoc like this:
# first line comment
# second line comment
def foo
end
When I output document by rdoc foo.rb, then line break are ignored in HTML file.
To add line break I can write like:
# first line comment<br>
# second line comment
or
# first line comment
#
# second line comment
but I feel both way are not simple enough.
Is there other simple way to add line break in RDoc?
Just add two or more spaces to the end of the line and it will work.
#first comment
#second comment
def foo
end
The first line has 2 spaces after comment.
Add answer regarding add line break in common text:
Make heading with full white space
 "===  " # <= the quoted part
At least this works on github.

Ruby: syntax for string interpolation and commenting

There are a lot of similar questions to this one but i think none really had the answer i was in need of.
From what i perceived, anything that has a # preceding it becomes a comment, as in:
puts "not a comment"
# puts "in a comment"
but when it come to string interpolation, you can say:
puts "time is #{Time.now}"
and the # at #{Time.now} doesn't start commenting whatever is written after it.
How is it so?
The # sign in this case is encased in quotation marks, and is parsed as part of the string, not as a comment.
Yes, what #Osama said. But also, if you put a comment inside the chunk of Ruby code, it won't comment out the rest of the entire line. So,
puts "'#{'oof'#.reverse},' he said."
wil print "'oof,' he said", while
puts "'#{'oof'.reverse},' he said."
results in "'foo,' he said".

Undoing auto-indentation

Wnen you use irb with auto indent mode, the end statements get indented one level extra
def foo
...
end
instead of showing the ordinary indenting convention:
def foo
...
end
because you cannot tell irb in advance that you are going to escape one level in the next line. This question has been addressed elsewhere like here or here, but neither gives a satisfactory answer. They just suggest giving up.
However, if we can minimally overwrite some irb methods so that auto indent will insert white spaces not in the prompt area but at the beginning of the line you type in, then by default, irb will still be inserting spaces, but we will be able to erase some spaces with backspace. Is this possible?
Or, if that is not realistic, then is it possible to make irb erase the last line from the screen and redisplay it with proper indentation right after you press Enter on a line including end?
Rewriting the last line is possible. Doing it in irb is difficult due to its lack of documentation and consistent api across versions. An irb alternative, ripl, has already solved this issue for itself with an auto-indent plugin. If you want to give ripl and its auto-indenting a try:
$ gem install ripl-auto_indent
$ echo "require 'ripl/auto_indent'" >> ~/.riplrc
# Auto-indent away
$ ripl
>> def foo
>> puts "it's auto-magic!"
>> end

Resources