Multi-Line Comments in Ruby

Multi-Line Comments in Ruby?

#!/usr/bin/env ruby

=begin
Every body mentioned this way
to have multiline comments.

The =begin and =end must be at the beginning of the line or
it will be a syntax error.
=end

puts "Hello world!"

<<-DOC
Also, you could create a docstring.
which...
DOC

puts "Hello world!"

"..is kinda ugly and creates
a String instance, but I know one guy
with a Smalltalk background, who
does this."

puts "Hello world!"

##
# most
# people
# do
# this


__END__

But all forgot there is another option.
Only at the end of a file, of course.
  • This is how it looks (via screenshot) - otherwise it's hard to interpret how the above comments will look. Click to Zoom-in:

Comments in a text-editor

Block commenting in Ruby

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

How to comment Multiple lines in rails 5 controller?

Ruby multi line comments only work when there is no whitespace between the start of the line and the =begin (the same applies to the =end). Make sure the line starts with =begin:

This works:

=begin
foo
bar
=end

This won't work:

  =begin
foo
bar
=end

Is it OK to use multi line regular expression to comment out code in Ruby?

Using Tadman's example

def example
do_important_stuff!
/
other(thing: true)
do_super_important_stuff(factor: 9000)
/
do_other_stuff
end

This creates a Regex object every time the method is called, which is not free. It takes time, memory and extra work for the garbage collection process.

Add comment to line in multiline %w in ruby

I think there is no way to make that work, because %w() evaluates every space delimited element inside it to string.

There's no way from inside the string to make Ruby evaluate that string.



Related Topics



Leave a reply



Submit