Multi-Line Comments in Ruby?

463,513

Solution 1

#!/usr/bin/env ruby

=begin
Every body mentioned this way
to have multiline comments.

The =begin and =end must be at the beginning of the line or
it will be a syntax error.
=end

puts "Hello world!"

<<-DOC
Also, you could create a docstring.
which...
DOC

puts "Hello world!"

"..is kinda ugly and creates
a String instance, but I know one guy
with a Smalltalk background, who
does this."

puts "Hello world!"

##
# most
# people
# do
# this


__END__

But all forgot there is another option.
Only at the end of a file, of course.
  • This is how it looks (via screenshot) - otherwise it's hard to interpret how the above comments will look. Click to Zoom-in:

Comments in a text-editor

Solution 2

=begin
My 
multiline
comment
here
=end

Solution 3

Despite the existence of =begin and =end, the normal and a more correct way to comment is to use #'s on each line. If you read the source of any ruby library, you will see that this is the way multi-line comments are done in almost all cases.

Solution 4

#!/usr/bin/env ruby

=begin
Between =begin and =end, any number
of lines may be written. All of these
lines are ignored by the Ruby interpreter.
=end

puts "Hello world!"

Solution 5

Using either:

=begin
This
is
a
comment
block
=end

or

# This
# is
# a
# comment
# block

are the only two currently supported by rdoc, which is a good reason to use only these I think.

Share:
463,513
Mohit Jain
Author by

Mohit Jain

warning ! This is just a rip-off my linkedin profile :D Seasoned Web Developer with over 8 years of work experience, with focus on code quality, scaling, timely delivery, under pressure working experience and performance optimization. My responsibilities in the past ranged from designing and implementing large scalable systems, managing and monitoring clusters of servers, and also mentoring junior engineers and managing project teams. I'm always interested in hands-on contributions to challenging and innovative projects. Good interpersonal skills. Uncompromising work ethic and integrity. Known for quickly ramping up on new code bases and incorporating massive design changes in existing systems. Clean and efficient programming style. Excellent debugging practices, used to work in code written by different people. In the last few years, I wrote tens of thousands of lines of code to scale a system from 1 million users to 20 million users single-handedly. Specialities: Ruby on Rails, Redis, Memcache, MySQL, New Relic, Amazon Web Services, Over night prototyping, Mixpanel

Updated on July 08, 2022

Comments

  • Mohit Jain
    Mohit Jain almost 2 years

    How can I comment multiple lines in Ruby?

  • PJP
    PJP over 13 years
    I really prefer using # over them all, mostly because it visually separates the commented lines better than =begin/=end or using the here-to method. And, nice job.
  • PJP
    PJP over 13 years
    You might get arguments about the "more correct" part of your statement as they're both valid. I prefer using # because it's more obvious. When commenting out code it's important to make it obvious that's what happened. If you're viewing the code without the benefit of code coloring in an editor using =begin/=end can make it tough to figure out why code is being ignored.
  • David J.
    David J. almost 12 years
    Sure, there are many "valid" ways to write comments. Let's be practical here. If you actually write Ruby and read what others write, you should be using # comments. (I am mystified why this had two downvotes. I guess the Stack Overflow community has to get it wrong sometimes!)
  • David J.
    David J. almost 12 years
    3 == three where def three; 1 + 1 + 1 end. Therefore both are valid. Who cares? Use 3!
  • David J.
    David J. almost 12 years
    Sure, you could do this. It works. This is incredibly rare. I find it ugly. Maybe I'm stuck in my ways?
  • David J.
    David J. almost 12 years
    For me, in Ruby 1.9.3p194, using the heredoc style as shown above gives as warning: possibly useless use of a literal in void context.
  • Rose Perrone
    Rose Perrone almost 12 years
    I've found that if I include a tab before =begin or =end, the comments don't work. The =begin and =end each need to be written at the beginning of each line.
  • ZoFreX
    ZoFreX over 11 years
    It's interesting that this answer makes some flaws in the syntax highlighter obvious.
  • bergie3000
    bergie3000 about 11 years
    Don't forget that =begin and =end cannot be preceded by any whitespace.
  • Albert Català
    Albert Català over 10 years
    And It is not possible to use =begin =end within a method
  • Eric
    Eric over 10 years
    If you're reading Ruby code in an editor that highlights rdoc comments correctly, they stand out, and your comments are less cluttered with a "#" starting every line. What's not to like? OTOH, a scan through the std library shows two hits, and a small smattering amongst my gems. OTOH, almost all Perl code uses POD. Maybe because the tools for generating HTML and man pages from Perl POD comments are so much further along than Ruby's.
  • Admin
    Admin almost 10 years
    I would prefer to use #, but most of the time I use =begin ... =end because I'm too lazy to type all those # or learn how to do block comments in vim
  • Parthian Shot
    Parthian Shot over 9 years
    @theTinMan While true, generally the only time you'd lack syntax highlighting (in my experience) is when you're using vi on a production server. In which case, you probably shouldn't be doing your development there, anyway.
  • Parthian Shot
    Parthian Shot over 9 years
    @DavidJames Your example is ridiculous because it's more verbose. Putting a hash on every line is more verbose for longer comments. And if anyone thinks the phrase "/dev/urandom was used here for the nonblocking cryptographically-sound PRNG. Do not touch this code- it is magic" is my attempt at writing ruby, I would contend their confusion arises more from ignorance on their part than lack of clarity on mine. Which isn't to say your point is always invalid- it's just only a good one when commenting out code. But if your comment is just... comment... it should be clear either way.
  • Parthian Shot
    Parthian Shot over 9 years
    +1 because I had no idea nesting was a thing in Ruby multiline comments.
  • PJP
    PJP over 9 years
    @ParthianShot, even on my production hosts I have my standard ~/.vim defined. And some of my production hosts are for development by the users.
  • PJP
    PJP over 9 years
    It's important to note that in the above example code, only the first =begin...=end and last block using # are picked up by rdoc when generating documentation.
  • Parthian Shot
    Parthian Shot over 9 years
    @theTinMan I am experiencing a mixture of confused, hungry and tired. One of those emotions is a response to your comment. The default highlighting in vim usually works well. When don't you have highlighting? EDIT: It was confused. Took me a bit but I got it sussed.
  • PJP
    PJP over 9 years
    You don't get default highlighting with a stripped vim on a ssh session with a terminal mismatch, or where vim doesn't understand the file type, especially on our hosts which are way out of date. My first task on any machine I help manage is to update vim from source to the latest full version just because I can't stand stripped vim but some machines I use I don't manage.
  • David J.
    David J. over 9 years
    IMO, =begin and =end do not visually convey that what is in-between is a comment... Clojure, for example, uses (comment :whatever) which at leads says what it means: stackoverflow.com/questions/1191628/block-comments-in-clojur‌​e
  • La-comadreja
    La-comadreja over 9 years
    Neither do "/*" and "*/" in Java, C and C++. As with the Ruby syntax, large blocks of code might be commented out between those two characters, and everyone who knows the basics of the language knows what they mean.
  • skagedal
    skagedal about 9 years
    @ParthianShot - It is not a thing - =begin and =end are ignored if not at the beginning of a line. Nesting does not seem to be possible.
  • Shayne
    Shayne about 8 years
    This isn't actually an answer to his question, hence the downvotes.
  • Chinoto Vokro
    Chinoto Vokro almost 8 years
    Nesting a comment inside a comment would result in either a single comment or a syntax error from trying to end a comment where there is no comment to end. /*I am a\n#nested\ncomment, which really serves no purpose*/ /*I am bound /*to*/ FAIL!*/ It could make sense if you have single line comments and code inside of a multiline comment, such as a function with documentation that you don't want people to use, but you also don't want to remove it from the file.
  • fIwJlxSzApHEZIl
    fIwJlxSzApHEZIl over 7 years
    you're not alone @DavidJames. I've personally opted to have them all commented out by my editor. CMD+/ or ALT+/ is the convention for most.
  • Paul Draper
    Paul Draper over 7 years
    @DavidJames, what would you do instead? Type a # and space before every single line? It's a lot of keystrokes especially if I start adding line breaks.
  • Camille Goudeseune
    Camille Goudeseune about 7 years
    Syntax coloring (in vim, for example) shows that the first type is a comment. In that case, the first type has no disadvantages.
  • Cyril Duchon-Doris
    Cyril Duchon-Doris over 5 years
    the Heredoc format <<-DOC will interpolate so you can't use #{} variables inside easily. and if you try to use <<-'DOC' (which basically tells heredoc not to perform interpolation), it will most likely screw your syntax highlighting.
  • Cœur
    Cœur almost 5 years
    <<-DOC will also create a String instance. So best to stick to # or =begin.
  • Cœur
    Cœur almost 5 years
    Another good reason to stick to =begin or # is that both <<-DOC and " syntaxes will generate useless string literals at execution.
  • Robert
    Robert over 3 years
    The multi line comment with begin and end, like your first code snippet, actually works.
  • Admin
    Admin almost 3 years
    @AlbertCatalà: And It is not possible to use =begin =end within a method I tested, and it seems to work within a method. However, you can't indent it. What did you have in mind when you said "not possible?" Do you mean that it doesn't work with rdoc or something?
  • Sampson Crowley
    Sampson Crowley over 2 years
    @BenCrowell that is correct AlbertCatala is wrong. begin end block comments can be used anywhere. the only restriction is that they cannot contain any superceding whitespace on =begin or =end so they are very ugly unless at the root of a file