AUTHORS
CATEGORIES
- Airsoft (2)
- Austin (8)
- Books (9)
- Cooking For Engineers (20)
- Credit Cards (2)
- Current Events (9)
- Deals (4)
- Dining (37)
- Fanpop (4)
- Food (808)
- Games (5)
- Hairy (6)
- Kitchen Gear (7)
- Life (24)
- Miscellaneous (6)
- Movies (15)
- Orthogonal Thought (6)
- Personal Computers (10)
- Photography (158)
- Rant (9)
- Ruby on Rails (2)
- San Francisco (5)
- Soap Making (5)
- Television (11)
- Texas (1)
- Things We Like (11)
- Travel (11)
- Wear or Not (1)
- Web 2.0 (9)
- What I Ate (861)
- Wikipedia (5)
- Windows 10 (3)
- Windows Mobile (1)
- WordPress (1)
ARCHIVE
- September 2018 (1)
- September 2017 (1)
- March 2017 (1)
- September 2016 (2)
- August 2016 (2)
- September 2015 (1)
- November 2014 (1)
- August 2014 (1)
- January 2014 (1)
- September 2013 (1)
- December 2012 (1)
- November 2012 (3)
- September 2012 (1)
- August 2012 (2)
- June 2012 (2)
- February 2012 (1)
- January 2012 (4)
- December 2011 (4)
- October 2011 (2)
- September 2011 (5)
- August 2011 (1)
- July 2011 (2)
- April 2011 (2)
- February 2011 (5)
- January 2011 (12)
- December 2010 (37)
- November 2010 (30)
- October 2010 (25)
- September 2010 (30)
- August 2010 (32)
- July 2010 (34)
- June 2010 (31)
- May 2010 (35)
- April 2010 (35)
- March 2010 (33)
- February 2010 (34)
- January 2010 (33)
- December 2009 (30)
- November 2009 (31)
- October 2009 (30)
- September 2009 (31)
- August 2009 (34)
- July 2009 (34)
- June 2009 (34)
- May 2009 (33)
- April 2009 (32)
- March 2009 (42)
- February 2009 (38)
- January 2009 (57)
- December 2008 (40)
- November 2008 (30)
- October 2008 (33)
- September 2008 (22)
- August 2008 (5)
- July 2008 (5)
- June 2008 (11)
- May 2008 (13)
- April 2008 (10)
- March 2008 (29)
- February 2008 (18)
- January 2008 (28)
- December 2007 (1)
- November 2007 (4)
- October 2007 (5)
- September 2007 (10)
- August 2007 (14)
- July 2007 (10)
- June 2007 (9)
- May 2007 (21)
- April 2007 (21)
Ruby multi-line comments
Posted 22 May, 2007 at 12:05am by Michael Chu(Filed under: Ruby on Rails, Web 2.0)
For over a year now, I've been working in Ruby on Rails (mostly for Fanpop and more recently for Cooking For Engineers) and one of the things that I wished I could do in Ruby was block comments. Internet searches I performed last year didn't yield anything, so I gave up. Well, last week, I looked around again and found a solution.
Comments in ruby are handled by preceding the comment with a hash (#):
# This whole line is a comment
x = Array.new # tailing comment
But what happens if you want to comment more than one line at a time? In C, comments are always block comments: /* comment */
and can span multiple lines. In C++, you have both single line comments ( // a single line comment
) and block comments ( /* comment */
), so why does Ruby have only single line comments?
When I last looked for multiple line comments in Ruby, I found a lot of websites and forums where the "solution" was to use an editor or IDE that lets you highlight multiple lines and insert (and remove) hashes from the beginning of each line with just a couple keystrokes. That's fine, but what if you're not using an editor that does that and you want to quickly comment out a couple blocks of code for testing purposes? I ended up resorting to the use of if false
:
if false
code that I want to be ignored
...
last line that I want ignored
end
There are several problems with this technique of which two are most important:
1. The code between if false
and end
must be syntactically correct. That means if you have any comments, you have to comment it out with a hash and your code blocks must be properly nested and closed. If you don't the parser won't be able to properly figure out what to do with the code.
2. Initializing variables inside the block will actually result in those variables being initialized even if you expect the block to never be executed or interpreted. For example, if you have a hash with multiple value pairs set called params and then you have the following code, don't be surprised if params is cleared:
if false
params = Hash.new
end
So you do you do comment blocks? Well, it turns out Ruby supports two types of comments - implementation comments (which use hashes, #) and documentation comments. Documentation comments start with =begin
and end with =end
. Typically, these two lines need to be at the beginning of the line (without leading spaces or tabs), but your mileage may vary.
=begin
code that is now commented out
...
doesn't matter what is in here...
could be gibberish or real code
=end
Magic. There is a problem with this: =begin
and =end
comments are supposed to be for documentation use (to generate documentation using a tool like rdtool). If you're using the comments to quickly comment out a large block of code for testing or you don't use embedded documentation, then =begin
and =end
could be very useful to you.
10 comments to Ruby multi-line comments
Chris, December 31st, 2007 at 2:36 pm:
-
I am a returning programmer, but the Ruby book I bought still (assumed) that I would read between the lines as I over embellish "Hello Matz".
I learn best by digging as deep as possible and take the "hello world" to a new level. All this being said, I was disappointed that my book failed to mention this small point of the block comment must not have any leading spaces or tabs as stated clearly (here, "Typically, these two lines need to be at the beginning of the line (without leading spaces or tabs),…".
Thank you Orthogonal Thought, et al.
Chris…
Jonah Dempcy, June 5th, 2008 at 5:49 pm:
-
I can't believe this is the only option! Are there are any plans for Ruby to support multi-line comments?
Conversely, I wish CSS would support single-line comments for the same reason. In CSS, I often want to comment out large blocks of code but cannot, because it's full of comments which will cause my intended comment to end prematurely. It isn't fair! Such a simple, basic feature should be a requirement for any modern programming language. Is there any reason not to support both single- and multi-line commenting?
Michael Chu, June 6th, 2008 at 11:43 am:
-
Yeah, kind of ridiculous isn't it? Many editors that understand Ruby will insert hashes (#) in front of each line of a block that you highlight and mark for commenting, but I like to use text editors and not IDE's for programming…
Russell Ianniello, February 19th, 2009 at 8:17 pm:
-
"but you’re mileage may vary."
your
Michael Chu, February 19th, 2009 at 11:40 pm:
-
Fixed the "you're" -> "your" typo.
Peter Kappus, August 10th, 2010 at 8:11 am:
-
Don't forget you can do this: (a perl relic, I believe)
=begin
Between =begin and =end, any number
of lines may be written. All of these
lines are ignored by the Ruby interpreter.
=endThanks to Michael Morin @ About.com
http://ruby.about.com/od/rubyfeatures/a/comment.htm
Michael Chu, August 10th, 2010 at 10:55 am:
-
Isn't the whole =begin =end construct what this post was about?
Matthias Lüttgert, December 30th, 2010 at 2:42 am:
-
<<EOC
This is my comment and
any stuff goes here.
To stop the comment, EOC has to be at the outset of the line!
EOCEOC may be replaced by EOF or any literal.
Ruby multi-line comment out – Richard Choi, April 4th, 2011 at 2:32 am:
Laszlo, September 27th, 2011 at 7:21 am:
-
->Mathias
Your solution has a drawback. The multi-line strings work similarly to the double quotes.
So If the commented out code contains one or more of these:
#{_code_}
then Ruby will still try to execute the _code_ blocks and if the variables are not defined within those _code_ blocks then you get an error message.