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
BeginnerFix 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 codeMulti-line comment:
=begin
This is a
multi-line comment
=endDisabling code:
# puts "This will NOT run"
puts "This will run"Try it yourself
# Type your code below
? 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