Commenting in Ruby
Oddly I have only just come across this after months of Ruby.
A line comment in Ruby is done using #. e.g.
uncommented_code
# commented code
uncommented_code
Block comments though I had to go look up. Apparently the inventor of Ruby isn’t keen on block comments and so it is done in the following cumbersome manner:
uncommented_code
=begin
commented code
and another line
=end
uncommented_code
May 9th, 2006 at 7:30 pm
Pretty much most people don’t even use the =begin/=end blocks any more. I just write stuff like
# This function adds two numbers together.
# No overflow checking is provided.
def sum(a,b)
a + b
end
It’s easier to read and makes the comments more discernible.
May 10th, 2006 at 7:24 am
In normal code I agree, line comments work better. But in this case, and often enough, I had 15 lines of code I wanted to exclude to check something.
Block comments are useful during development.
May 10th, 2007 at 10:25 pm
If you use textmate on the mac, you can highlight a block of code and hit command + / to comment it and then again to remove the comments. Or on vim shift + v and cursor down to select the line then s/^/#/g, phew!
I agree though, it can be painful after spending years using /* */ comments. IMHO, its one of the very few annoyances I have with ruby.
May 21st, 2007 at 8:12 am
Depending on your editor, you can also use the # comment easily to block comment by using rectangular select, eg in jedit alt+/ will toggle triangular select, then shift+down arrow at the beginning of the lines you want to comment, then a single # will put a hash at the beginning of all the lines. Similarly to uncomment, simply delete the # using the same select method. Once you get the hang of it, it’s really easy.
PS the rectangular select is brilliant, once you start using it, you’ll wonder how you ever managed without it!
December 18th, 2007 at 12:34 am
Do you know if there is any way to do block comment on view block? I mean on the rhtml file?
Thanks
December 20th, 2007 at 6:56 am
Brian,
For block comment, you do:
=begin
Your code
=end
I hope you find that useful
Thanks
December 20th, 2007 at 8:28 am
Does that work in RHTML/ERB files though Los Angeles Electricians?
December 21st, 2007 at 4:15 pm
No, That does not work.
In Rhtml or ERB I would use :
Do you have alternatives?