Nested multiline comments in ruby? - 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?

Related

What's the difference between keyword_end and end-of-input in ruby?

Sometimes, I ran into an error like this in ruby:
syntax error, unexpected keyword_end, expecting end-of-input
So, I want to know the difference between keyword_end and end-of-input
Keyword 'end' is the end statement in Ruby that comes at the end of methods, classes, etc. end-of-input is the actual end of your code file. The message you got means you have an extra 'end' statement somewhere.
keyword_end implies that you had a block of code with a missing end.
def fun(f)
puts "Oops!"
End-of-input implies that you have already closed up blocks of code with one too many end keywords.
def fun(f)
puts "Okay..."
end
end

So many errors 'Bareword found where operator expected' occured in my ruby script

I upgraded ruby from 1.9.3 to 2.0.0
After upgrading, I cannot run my script anymore because there are so many errors.
UPDATE
I updated the beginning part of my script.
errors
Bareword found where operator expected at D:\ex\report.rb line 12, near "$0
def"
(Missing operator before def?)
Bareword found where operator expected at D:\ex\report.rb line 18, near "usage"
(Missing semicolon on previous line?)
Semicolon seems to be missing at D:\ex\report.rb line 19.
syntax error at D:\ex\report.rb line 5, near "Encoding::UTF_8 require "
Execution of D:\jenkins_lab_a\asap.ex\asap-report-apps.rb aborted due to compilation errors.
script
# coding: utf-8
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
require './lib/utils'
require './lib/klocwork'
require './lib/td'
require 'fileutils'
require 'logger'
if __FILE__ == $0
def usage
warn 'usage: report.rb [a] [b] [c]'
warn "ex) report.rb a b c"
exit
end
puts "report.rb start"
usage if ARGV.size < 3
end
I can see errors like this all over the script.
But I never changed my script and It is working well in the previous version.
So I think that I missed any environmental settings
Could you let me know what I can check to resolve this issue?
Those aren't Ruby error messages. You aren't running your Ruby files with a Ruby interpreter. There's no concept of "bareword" in Ruby, and semicolons are entirely optional, so it wouldn't report about them either.

Seeing numbers in Ruby with r attached to the number

When you come across numbers that look like this in Ruby code, what does it mean?
1r
Or
1.0r
Tested in Ruby 1.8.6 up to 2.0.0 and it fails. with something like this:
>> 1r
SyntaxError: unexpected tIDENTIFIER, expecting end-of-input
and
>> 1.0r
SyntaxError: unexpected tIDENTIFIER, expecting end-of-input
Probably downvoted for not searching, or something, or not enough examples. Hopefully this clarifies. It is just Ruby Code, it doesn't have to be specifically anywhere, just has to be there.
That is a new feature for Decimal/Rational Literals in Ruby 2.1. See here: http://rkh.im/ruby-2.1 (search for "Decimal Literals")
0.1r #=> (1/10)
0.1r * 3 #=> (3/10)

Is there a quick way to find missing end’s in Ruby?

syntax error, unexpected $end, expecting keyword_end
We’ve all been there! Assuming enough code changed that a quick glance at git diff or the like doesn’t make it obvious, is there an easy way to find that missing end (short of switching to an indentation-based language like Python)?
FWIW, I use Sublime Text 2 as my editor.
If you're using Ruby 1.9, try the -w flag when running your ruby program.
# t.rb
class Example
def meth1
if Time.now.hours > 12
puts "Afternoon"
end
def meth2
# ...
end
end
ruby t.rb
=> t.rb:10: syntax error, unexpected $end, expecting keyword_end
ruby -w t.rb
=> t.rb:5: warning: mismatched indentations at 'end' with 'if' at 3
t.rb:9: warning: mismatched indentations at 'end' with 'def' at 2
t.rb:10: syntax error, unexpected $end, expecting keyword_end
Source:
http://pragdave.blogs.pragprog.com/pragdave/2008/12/ruby-19-can-check-your-indentation.html
Edit (2023): this is now part of Ruby 3.2 and doesn‘t need to be required separately!
————————————
Edit (2022): the gem is now called syntax_suggest and is part of the Ruby standard library!
————————————
Since December 2020, there is also the dead_end gem which helps you detect missing ends.
The easiest way to get started is to install the gem:
gem install dead_end
and run it directly from your console, providing the file name to scan:
dead_end myfile.rb
This will provide you with a more helpful error message:
43 if (col_idx - 1) % 3 == 0
50 if !contains?(doc, node)
❯ 51 doc.add_child(node)
❯ 52 tree.
53 end
54 end
See the documentation for more options.
If you're using rails, you can run the problematic file specifically with the -w flag.
If you still can't find the offending mismatch, then my go-to way of solving this problem is just commenting out chunks of code until I can isolate the problematic area.
On OS X you can use command + / to comment out the highlighted piece of text.

Exact specification for block comment

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

Resources