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:
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
How to Deal With the Sum of Rounded Percentage Not Being 100
Rails Model, View, Controller, and Helper: What Goes Where
How to Convert Json to a Ruby Hash
Why Isn't Current Directory on My Ruby Path
Stop Devise from Clearing Session
Getting "Warning! Path Is Not Properly Set Up" When Doing Rvm Use 2.0.0 --Default
Rufus Scheduler Not Running in Production
Ruby Differences Between += and << to Concatenate a String
Passing Block into a Method - Ruby
Gem Install Therubyracer' Fails on MAC Os X Lion
How to Change the Default Path of View Files in a Rails 3 Controller
Ruby/Rails - Change the Timezone of a Time, Without Changing the Value
Can't Install Pg Gem on Windows
Why Do Two Strings Separated by Space Concatenate in Ruby
How to Update Ruby to 1.9.X on Mac
Limit Space and Memory Used by Imagemagick
Ruby:Difference Between Instance and Local Variables in Ruby