Block Comments in Ruby

This is an easy one. How do you comment out a chunk of code? Other programming languages (C++, Java, etc.) have a slash-star style of block comments, like so:


/*
This is a block comment. You can put whatever you want here.
Even ASCII art! For REAL! o[+++]XXXXXXXXXXXX>
*/
// ...

But what about Ruby?

Ruby has two special tags that you use: =begin and =end. So your Ruby comment would look like this:


=begin
This function returns the minimum of A or B.
For example, if A is 3 and B is 7, it'll return 3.
=end
def self.min(a, b)
# ...
end

Yes, yes! It’s easy!

Wait! What’s that, you say? You say it doesn’t work? Well, that’s tragic–and in fact, it happened to me, too. The key is that these tokens cannot be preceded by whitespace. If they are, it won’t work.

What? Ghetto, you say? Well, tough luck, my friend–this is how Ruby deals with block comments. Just use it, OK? It’s easier than commenting out 130+ lines of code one by one. (Or if you have an IDE that does it in one-shot, great, even better!)

And that’s Ruby comment blocks! Simple!

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , , . Bookmark the permalink.

2 Responses to Block Comments in Ruby

  1. ı dont understand

  2. Thanks, very nice..

Comments are closed.