Multiline comments in Ruby.

March 17, 2009 at 1:33 am 3 comments

Maybe the Google failed me, maybe I was just not being observant, but when I first started using Ruby, I could not for the life of me figure out how to make multiline comments.  I had resorted to using lots of # signs, which isn’t especially difficult, but it just felt like there had to be an easier way.

Turns out, there is an easier, although perhaps still a bit clunky, way.  To start a multiline comment put =begin before the first line of comments and =end after the last line.  Note that these commands can not be indented at all, you have to put them at the beginning of the line.

Advertisements

Entry filed under: Potentially Useful Stuff.

How not to impress your boss. PMI TMI

3 Comments Add your own

  • 1. Deepak  |  November 29, 2010 at 12:12 pm

    Thanks for this. The tip about putting the =begin right at the start of the line was really helpful!

    Reply
  • 2. Henway  |  January 19, 2012 at 5:17 am

    Thanks for this.. I read in many places to use =begin and =end, but it never seemed to work so I resorted to using #’s. Thenone day it got too much frustrating and I vowed to find out why it never worked. Turned out I needed to put the =begin in beginning of line. Too many ppl forget to mention this!!! You save me! Kudos to you. Hope you get into heaven and/or get lots of good sex in this life, my friend

    Reply
  • 3. bozaloshtsh  |  October 9, 2012 at 9:37 pm

    Actually, you can also use a messy trick that lets you write a comment that looks similar (but not exactly like) a Java/C/C++ comment:

    /.* Ruby multiline “comment”!
    .* line 2
    .* line 3
    .*/ #end of “comment”

    Note that the “comment” is actually a regex. This is only to be used in the most dire situations in which you *need* to write a comment Java-style.

    Don’t start or end these on lines where there is already a statement to avoid unexpected results.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

Recent Tweets


%d bloggers like this: