Menu
Coddy logo textTech

Comments in Ruby

Part of the Fundamentals section of Coddy's Ruby journey — lesson 3 of 88.

Comments are notes you write inside your code. Ruby completely ignores them - they exist only to help humans understand the code.

To write a single-line comment, use the # symbol. Everything after # until the end of the line is ignored:

# This is a comment
puts "Hello!"

A comment can also be written at the end of a line, after the code:

puts "Hello!" # This prints Hello!

For comments that span several lines, use =begin to start and =end to end, each on its own line:

=begin
This is a multi-line comment.
Ruby ignores all of it.
=end
puts "Welcome!"

Comments can also temporarily disable a line of code without deleting it:

# puts "This line will NOT run"
puts "This line will run"
challenge icon

Challenge

Beginner

Fix the code so that only Hello, Ruby! is printed.

  • Replace the ? with the symbol that turns a line into a comment
  • The line printing Goodbye! should become a comment so it does NOT run
  • Only change the line that starts with ?

Cheat sheet

Comments in Ruby:

  • Notes for humans - Ruby ignores them
  • Can disable code temporarily without deleting it

Single-line comment:

# This is a comment
puts "Hello!" # Comment after code

Multi-line comment:

=begin
This is a
multi-line comment
=end

Disabling code:

# puts "This will NOT run"
puts "This will run"

Try it yourself

# Type your code below
? puts "Goodbye!"
puts "Hello, Ruby!"
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals