TExcelTAD
Home › Ruby › Ruby Comments

Ruby Comments

Comments are notes for humans. Ruby ignores them completely when running the program. Good comments explain why the code does something; the code itself should already show what it does.

Single-line comments

Everything after a # on a line is a comment (unless the # is inside a string):

ruby · try it yourself
# Calculate the price of a cinema ticket
base_price = 12.5
discount = 0.2 # students get 20% off

puts base_price * (1 - discount)
puts "A # inside a string is just a character"
10.0
A # inside a string is just a character

A comment can sit on its own line or at the end of a line of code. Leave one space after the # — it reads better.

Commenting out code

A quick way to disable a line while experimenting is to put # in front of it:

ruby · try it yourself
puts "Step 1"
# puts "Step 2 (disabled for now)"
puts "Step 3"
Step 1
Step 3

Most editors toggle comments on the selected lines with Ctrl+/ (or Cmd+/ on macOS).

Multi-line comments

The usual way to write a longer comment is several lines that each start with #:

ruby · try it yourself
# This method converts a temperature from Celsius
# to Fahrenheit. The formula multiplies by 9/5
# and then adds 32.
def to_fahrenheit(celsius)
  celsius * 9.0 / 5 + 32
end

puts to_fahrenheit(25)
77.0

Ruby also has a block-comment syntax with =begin and =end. Both markers must start at the very beginning of a line:

ruby · try it yourself
=begin
Everything between these two markers
is ignored by Ruby.
puts "this never runs"
=end
puts "Only this line runs"
Only this line runs

Note: =begin/=end works, but most Ruby style guides prefer stacked # lines because they are easier to see and indent.

Magic comments

A few special comments at the top of a file change how Ruby reads it. The most common one is:

ruby · try it yourself
# frozen_string_literal: true

greeting = "hello"
puts greeting.frozen?
true

With this magic comment, every string literal in the file is frozen (cannot be changed in place), which saves memory and prevents accidental changes. You will learn more in the Freezing & Immutability lesson.

Magic comment Effect
# frozen_string_literal: true String literals in this file are frozen
# encoding: utf-8 Source encoding (UTF-8 is already the default)
#!/usr/bin/env ruby "Shebang" line: lets Unix run the file directly as ./script.rb

Documentation comments

Libraries often document their methods with structured comments that tools such as YARD or RDoc turn into web pages:

ruby
# Returns the total price including tax.
#
# @param amount [Float] the net price
# @param rate [Float] tax rate, e.g. 0.21
# @return [Float]
def with_tax(amount, rate)
  amount * (1 + rate)
end

Writing good comments

Instead of… Write…
i += 1 # add one to i No comment — the code is obvious
# loop above a loop A comment explaining why the loop is needed, if unclear
Long comment explaining confusing code Rename variables or extract a method so the code explains itself

Tip: Use # TODO: and # FIXME: markers for unfinished work. Many editors highlight them and list them for you.

Test yourself

Which character starts a single-line comment in Ruby?

  1. //
  2. #
  3. --
  4. ;

Where must =begin and =end be placed?

  1. Anywhere on a line
  2. At the very start of a line
  3. Inside a method only
  4. After #

What does # frozen_string_literal: true at the top of a file do?

  1. Nothing, it is just a comment
  2. Makes all string literals in the file frozen
  3. Freezes every variable
  4. Prevents the file from being edited