# Block comments syntax

**URL:** <https://www.ruby-forum.com/t/block-comments-syntax/51065>\
**Category:** Ruby\
**Created:** [December 25, 2005, 12:54pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065 "2005-12-25T12:54:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 25, 2005, 12:54pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/1 "2005-12-25T12:54:18Z")

</div>

Hello,

It says at [http://www.rubygarden.org/ruby?RubyFromC](http://www.rubygarden.org/ruby?RubyFromC) that I can do block  
comment in Ruby like so:

=begin  
my comment  
=end

However, when I try to I get a syntax error. Why is this? How can I do  
it?

Many Thanks

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 25, 2005, 1:39pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/2 "2005-12-25T13:39:24Z")

</div>

On Sun, 25 Dec 2005, Jonathan L. wrote:

> it?  
> I don’t see an error:

wybo\>cat t  
=begin  
my comment  
=end  
puts ‘hello’  
wybo\>ruby t  
hello  
wybo\>ruby -v  
ruby 1.8.4 (2005-10-29) [i686-linux]  
wybo\>

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 25, 2005, 1:45pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/3 "2005-12-25T13:45:26Z")

</div>

Make sure that you don’t indent them…

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 25, 2005, 6:15pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/4 "2005-12-25T18:15:57Z")

</div>

On Sun, 2005-12-25 at 21:45 +0900, Stefan L. wrote:

> - Ensure that there is no space/tab before =begin and  
> =end (i.e. the = must be the first character in the line)

This was it, thanks all.

Incidentally, the =begin and =end syntax seems pretty cumbersome to me  
(compared to /\* \*/ for instance). Is it like this because Ruby is so  
versatile and anything else would be difficult to parse?

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 25, 2005, 9:10pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/5 "2005-12-25T21:10:32Z")

</div>

More that it is a facility for embedded documentation:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/rdtool.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/rdtool.html)

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 25, 2005, 1:45pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/6 "2005-12-25T13:45:27Z")

</div>

On Sunday 25 December 2005 12:53, Jonathan L. wrote:

> do it?  
> Two notes, since the RubyGarden page doesn’t mention it:

- Ensure that there is no space/tab before =begin and  
=end (i.e. the = must be the first character in the line)

- Nesting of =begin/=end blocks is not possible.

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 28, 2005, 2:22pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/7 "2005-12-28T14:22:15Z")

</div>

On Wed, 2005-12-28 at 13:22 +0900, mathew wrote:

> Jonathan L. wrote:
> 
> > Incidentally, the =begin and =end syntax seems pretty cumbersome to me  
> > (compared to /\* \*/ for instance). Is it like this because Ruby is so  
> > versatile and anything else would be difficult to parse?
> 
> Maybe it’s like that because it’s a facility you’re not supposed to use;  
> many people feel block comments are a bad idea. Personally, I only use  
> them in languages which lack line comments.

Well if you want to temporarily get rid of a large number of lines,  
block comments are much, much quicker than doing a line comment for each  
and every line. I don’t tend to leave block comments in the app though  
– just during development I find them useful.

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 28, 2005, 8:08pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/8 "2005-12-28T20:08:11Z")

</div>

Jonathan L. wrote:

> On Wed, 2005-12-28 at 13:22 +0900, mathew wrote:
> 
> > Jonathan L. wrote:
> > 
> > > Incidentally, the =begin and =end syntax seems pretty cumbersome to me  
> > > (compared to /\* \*/ for instance). Is it like this because Ruby is so  
> > > versatile and anything else would be difficult to parse?
> > 
> > Maybe it’s like that because it’s a facility you’re not supposed to use;  
> > many people feel block comments are a bad idea. Personally, I only use  
> > them in languages which lack line comments.
> 
> Well if you want to temporarily get rid of a large number of lines,  
> block comments are much, much quicker than doing a line comment for each  
> and every line. I don’t tend to leave block comments in the app though  
> – just during development I find them useful.

Perhaps.

V10n:s/^/#/

Comments out the next ten lines for me. I could alias  
it to a macro key but I have not bothered yet 🙂

E

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 28, 2005, 5:23am UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/9 "2005-12-28T05:23:30Z")

</div>

Jonathan L. wrote:

> Incidentally, the =begin and =end syntax seems pretty cumbersome to me  
> (compared to /\* \*/ for instance). Is it like this because Ruby is so  
> versatile and anything else would be difficult to parse?

Maybe it’s like that because it’s a facility you’re not supposed to use;  
many people feel block comments are a bad idea. Personally, I only use  
them in languages which lack line comments.

mathew

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [December 30, 2005, 2:43pm UTC](https://www.ruby-forum.com/t/block-comments-syntax/51065/10 "2005-12-30T14:43:39Z")

</div>

Hello.

Jonathan L.:

> Well if you want to temporarily get rid of a large number of lines,  
> block comments are much, much quicker than doing a line comment for  
> each and every line.

In vim, I simply use block select (ctrl-v) to select the first column  
of the affected lines and replace the spaces with comment marks (r#).  
This has the additional benefit of not shifting the commented-out code  
to the right.

Cheers,  
– Shot
