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!

Tags: , ,     Posted in Development

Rate this article:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Related Content


2 Responses to “Block Comments in Ruby”

  1. halı yıkama Says:

    ı dont understand

  2. halı yıkama Says:

    Thanks, very nice..

Leave a Reply