Output with puts
Part of the Fundamentals section of Coddy's Ruby journey — lesson 26 of 88.
You've been using puts throughout this course to display messages. Now let's take a closer look at how it actually works.
The puts method (short for "put string") outputs text to the screen and automatically adds a new line at the end:
puts "Hello"
puts "World"This outputs:
Hello
WorldEach puts statement starts on a new line because of that automatic line break. You can also output multiple items at once by separating them with commas:
puts "Apple", "Banana", "Cherry"This outputs each item on its own line:
Apple
Banana
CherryThe puts method is the most common way to display output in Ruby because it keeps your output clean and readable with automatic line breaks.
Challenge
EasyUse puts to display the following information about a bookstore, with each item on its own line:
- Print
Welcome to Ruby Books! - Print the three book genres
Fiction,Mystery, andScienceusing a singleputsstatement with multiple items separated by commas - Print
Happy Reading!
Your output should have exactly 5 lines total.
Cheat sheet
The puts method (short for "put string") outputs text to the screen and automatically adds a new line at the end:
puts "Hello"
puts "World"Output:
Hello
WorldYou can output multiple items at once by separating them with commas, each on its own line:
puts "Apple", "Banana", "Cherry"Output:
Apple
Banana
CherryTry it yourself
# TODO: Write your code below
# Use puts to display the bookstore information
# Remember: puts with multiple items separated by commas prints each on its own lineThis 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