AUTHORS
- Michael Chu (298)
- Tina Jiang (3)
CATEGORIES
- Airsoft (2)
- Books (9)
- Cooking For Engineers (19)
- Current Events (9)
- Deals (3)
- Dining (13)
- Fanpop (3)
- Food (48)
- Games (4)
- Life (21)
- Movies (15)
- Orthogonal Thought (4)
- Personal Computers (5)
- Photography (77)
- Rant (6)
- Ruby on Rails (2)
- San Francisco (1)
- Soap Making (3)
- Television (2)
- Travel (3)
- Wear or Not (1)
- Web 2.0 (8)
- What I Ate (92)
- Windows Mobile (1)
- WordPress (1)
ARCHIVE
- December 2008 (2)
- 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)
ACTIONS
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 you’re 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.
3 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…







