What are comments in Ruby?
A Ruby comment adds information to your code that may be helpful for you or other developers.
The most common type of comment is the single-line comment.
Here’s the syntax:
# I like apples & oranges
Notice three things:
- The comment starts with a pound (
#
) symbol - We put a space between the contents of the comment & the start of the comment to make it easier to read
- Everything inside the comment is not interpreted as code, even if it looks like code. This means you can temporarily comment out code to disable it.
You can use these comments on their own, or inline, at the end of a line of code.
Like this:
[].size # get array size
Are there other kinds of comments?
Yes, there are magic comments, multiline comments & ERB comments.
Let’s explore those!
How to Use Multi-Line Comments in Ruby
In Ruby, we do multiline comments with regular, single-line comments.
Some people call these “block comments”.
Here’s an example:
# aaa # bbb # ccc
That’s exactly how to comment out a block of code in ruby.
Modern code editors allow you to select a block of code & comment all of it using a keyboard shortcut, so there is no extra effort involved.
But there’s a special multiline comment syntax.
It looks like this:
=begin This is a comment... a b c =end
Nobody uses this syntax…
It’s ugly & you can’t even use it inside anything that’s indented (like a method), but its good to know it exists if you ever find it in the wild.
SheBang Comments
A shebang (#!
) is a special kind of comment that tells a Unix shell (like bash) how to interpret this file.
When you add this comment at the top of your file…
You’ll be able to run your Ruby files as executable files, assuming they have the right permissions to do that.
Here’s what a shebang comment looks like:
#!/usr/bin/env ruby
This also allows you to set specific command-line options, like the warning option.
Example:
#!/usr/bin/env ruby -w
Every time you run this code it will run with these options, so you don’t have to pass them in manually.
How to Use Magic Comments
A magic comment changes the behavior of the Ruby interpreter in some way.
For example:
The frozen_string_literals
comment will make your strings frozen by default.
It looks like this:
# frozen_string_literal: true
Another magic comment allows you to change the file’s encoding.
Here’s an example:
# encoding: utf-8
A here’s an interesting one, the warn_indent comment:
# warn_indent: true
This will show a warning whenever your indentation is wrong.
Example:
def comments end
Which results in:
warning: mismatched indentations at 'end' with 'def' at 3
ERB Comments
If you’re using ERB for your Rails views & you need to add a comment…
Then you can use this syntax:
<%# ERB comment %>
I’m not a big fan of this because I believe you should keep most of your logic out of your views so you shouldn’t need any comments.
Looking to disable some code for debugging?
Then delete that code & trust git, or whatever version control system you’re using.
Summary
You learned about different kinds of Ruby comments, including regular comments (#
), shebang comments (#!
) & magic comments (# frozen_string_literal: true
)!
If you found this article helpful please share it with a friend so he/she can enjoy this too.
Thanks for reading!