Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block commenting in Ruby

Does Ruby have block comments?

If not, is there an efficient way of inserting # in front of a block of highlighted code in TextMate?

like image 684
alamodey Avatar asked Feb 11 '09 13:02

alamodey


People also ask

How do you comment out multiple lines of code in Ruby?

A multi-line comment begins with the =begin token and ends with the =end token. These tokens should start at the beginning of the line and be the only thing on the line. Anything between these two tokens is ignored by the Ruby interpreter. lines are ignored by the Ruby interpreter.

How do you write a block comment?

The first is called a single line comment and, as implied, only applies to a single line in the "source code" (the program). The second is called a Block comment and refers usually refers to a paragraph of text. A block comment has a start symbol and an end symbol and everything between is ignored by the computer.

How do I comment multiple lines in Ruby Mac?

Multiline comments Multiple-line comments can be added by using =begin and =end syntax (also known as the comment block markers) as follows: #!/usr/bin/ruby -w =begin This is a multiline comment.


3 Answers

You can do

=begin   [Multi line comment] =end 

=begin and =end must be at the beginning of the line (not indented at all).

Source

Also, in TextMate you can press Command + / to toggle regular comments on a highlighted block of code.

Source

like image 60
Garry Shutler Avatar answered Oct 01 '22 04:10

Garry Shutler


Ruby has documentation comments - they look like this:

=begin ... =end 

Not perfect but they get the job done in a pinch.

[Edit] It is important to note that =begin and =end must be at the beginning of their respective lines.

like image 21
Andrew Hare Avatar answered Oct 01 '22 03:10

Andrew Hare


In TextMate, you can alt-drag vertically to select a column of text. This will duplicate the insertion point across all the lines you select, so you can insert or delete multiple #s at once.

UPDATE: Also in TextMate, assuming you have the right language selected, Cmd + / will toggle commenting every line in a selection regardless of language.

like image 35
Andrew Avatar answered Oct 01 '22 05:10

Andrew