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"Note: Every multi-line comment that starts with =begin must be closed with =end, and both must be at the very start of their own lines.
Challenge
BeginnerHello, Ruby! is printed.- Replace the
?character entirely with the#symbol to turn that line into a comment. - The line printing
Goodbye!should become a comment so it does NOT run. - Ensure your final code looks like this:
# puts "Goodbye!"followed byputs "Hello, Ruby!".
Try it yourself
# Type your code below
# TODO: Comment out the line below by adding a # at the start
puts "Goodbye!"
puts "Hello, Ruby!"This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False