← Back to Ruby Course | Chapter 1: Setup & Basics | Lesson 6 of 7

Comments

A comment is a note for humans that starts with a hash mark and is ignored by Ruby. It is like a sticky note in the margin of a recipe.

In this page:

  1. Comments
Syntax
ruby
# single-line comment
value = 1  # trailing comment

=begin
block comment
=end

Comments

A # starts a single-line comment in Ruby, and everything after it on that line is ignored. Multi-line comments use =begin and =end, though single-line # comments are far more common in practice.

Comments should explain non-obvious reasoning, not restate what the code already makes clear.

Note: Most Ruby style guides discourage =begin/=end blocks in favor of multiple # lines.

Example: Comments

markup
# This script demonstrates comments
puts "Hello!"  # inline comment after code

=begin
This is a multi-line comment block,
though single-line # comments are more idiomatic.
=end

puts "Still running"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using // for comments, which is a syntax error in Ruby; use #.
  2. Putting =begin and =end after spaces, when they must start at the very beginning of the line.
  3. Forgetting =end, which comments out the rest of the file.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.