How can I comment multiple lines 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:
#
over them all, mostly because it visually separates the commented lines better than =begin
/=end
or using the here-to method. And, nice job. –
Radiance warning: possibly useless use of a literal in void context
. –
Fair =begin
and =end
cannot be preceded by any whitespace. –
Leigha =begin...=end
and last block using #
are picked up by rdoc when generating documentation. –
Radiance <<-DOC
will interpolate so you can't use #{}
variables inside easily. and if you try to use <<-'DOC'
(which basically tells heredoc not to perform interpolation), it will most likely screw your syntax highlighting. –
Kazukokb <<-DOC
will also create a String instance. So best to stick to #
or =begin
. –
Befool =begin
or =end
so they are very ugly unless at the root of a file –
Akene =begin
My
multiline
comment
here
=end
#
and space before every single line? It's a lot of keystrokes especially if I start adding line breaks. –
Koval Despite the existence of =begin
and =end
, the normal and a more correct way to comment is to use #
's on each line. If you read the source of any ruby library, you will see that this is the way multi-line comments are done in almost all cases.
#
because it's more obvious. When commenting out code it's important to make it obvious that's what happened. If you're viewing the code without the benefit of code coloring in an editor using =begin/=end
can make it tough to figure out why code is being ignored. –
Radiance #
comments. (I am mystified why this had two downvotes. I guess the Stack Overflow community has to get it wrong sometimes!) –
Fair 3 == three
where def three; 1 + 1 + 1 end
. Therefore both are valid. Who cares? Use 3
! –
Fair vi
on a production server. In which case, you probably shouldn't be doing your development there, anyway. –
Isidraisidro #!/usr/bin/env ruby
=begin
Between =begin and =end, any number
of lines may be written. All of these
lines are ignored by the Ruby interpreter.
=end
puts "Hello world!"
/*I am a\n#nested\ncomment, which really serves no purpose*/
/*I am bound /*to*/ FAIL!*/
It could make sense if you have single line comments and code inside of a multiline comment, such as a function with documentation that you don't want people to use, but you also don't want to remove it from the file. –
Thrashing Using either:
=begin This is a comment block =end
or
# This # is # a # comment # block
are the only two currently supported by rdoc, which is a good reason to use only these I think.
=begin
or #
is that both <<-DOC
and "
syntaxes will generate useless string literals at execution. –
Befool =begin
comment line 1
comment line 2
=end
make sure =begin
and =end
is the first thing on that line (no spaces)
Here is an example :
=begin
print "Give me a number:"
number = gets.chomp.to_f
total = number * 10
puts "The total value is : #{total}"
=end
Everything you place in between =begin
and =end
will be treated as a comment regardless of how many lines of code it contains between.
Note: Make sure there is no space between =
and begin
:
- Correct:
=begin
- Wrong:
= begin
=begin
(some code here)
=end
and
# This code
# on multiple lines
# is commented out
are both correct. The advantage of the first type of comment is editability—it's easier to uncomment because fewer characters are deleted. The advantage of the second type of comment is readability—reading the code line by line, it's much easier to tell that a particular line has been commented out. Your call but think about who's coming after you and how easy it is for them to read and maintain.
=begin
and =end
do not visually convey that what is in-between is a comment... Clojure, for example, uses (comment :whatever)
which at leads says what it means: https://mcmap.net/q/54861/-block-comments-in-clojure –
Fair In case someone is looking for an alternate way to comment multiple lines, for instance:
<%
=begin
%>
... multiple HTML lines to comment out
<%= image_tag("image.jpg") %>
<%
=end
%>
can be replaced with:
<% if false # The following section is commented out because... %>
... multiple HTML lines to comment out
<%= image_tag("image.jpg") %>
<% end # if false %>
This is not strictly commenting, because there is an if sentence, and therefore there is a very small use of the CPU, so I wouldn't use this kind of resource inside code that runs frequently.
However sometimes I find it more appealing.
Does anyone know if there is a difference in main memory usage with this practice?
def idle
<<~aid
This is some description of what idle does.
It does nothing actually, it's just here to show an example of multiline
documentation. Thus said, this is something that is more common in the
python community. That's an important point as it's good to also fit the
expectation of your community of work. Now, if you agree with your team to
go with a solution like this one for documenting your own base code, that's
fine: just discuss about it with them first.
Depending on your editor configuration, it won't be colored like a comment,
like those starting with a "#". But as any keyword can be used for wrapping
an heredoc, it is easy to spot anyway. One could even come with separated
words for different puposes, so selective extraction for different types of
documentation generation would be more practical. Depending on your editor,
you possibly could configure it to use the same syntax highlight used for
monoline comment when the keyword is one like aid or whatever you like.
Also note that the squiggly-heredoc, using "~", allow to position
the closing term with a level of indentation. That avoids to break the visual reading flow, unlike this far too long line.
aid
end
Note that at the moment of the post, the stackoverflow engine doesn't render syntax coloration correctly. Testing how it renders in your editor of choice is let as an exercise. ;)
© 2022 - 2024 — McMap. All rights reserved.