Ruby Academy · Lesson

User Input and Output

Capture user input from the console and format output using puts and gets.

Lesson 2 of 310 steps

User Input and Output is a free Ruby Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 3 lessons in total.

User Input and Output

Interacting with users through input and output is an essential part of many applications.

In this lesson, you will learn how to capture user input and format output using puts and gets.

User Input and Output — illustration 1

Displaying Output with puts

Use puts to print text to the console.

puts 'Hello, Ruby!'

Using print

print works like puts but does not add a new line.

print 'Hello, '
print 'Ruby!'

Getting User Input

Use gets to capture user input from the console.

puts 'Enter your name:'
name = gets
puts "Hello, #{name}"

Using gets.chomp

gets captures the newline character. Use chomp to remove it.

puts 'Enter your city:'
city = gets.chomp
puts "You live in #{city}."

Interpolating Variables in Strings

Use #{} inside double quotes to insert variables into a string.

name = 'Alice'
puts "Hello, #{name}!"

Getting Numeric Input

Use to_i to convert input into an integer.

puts 'Enter your age:'
age = gets.chomp.to_i
puts "In 5 years, you will be #{age + 5} years old."

Formatting Output

Use printf or sprintf to format output.

price = 9.99
printf('Price: $%.2f', price)

Congratulations!

You have learned how to capture user input and format output in Ruby.

Next, you will explore working with JSON and YAML.

User Input and Output — illustration 10
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “User Input and Output” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “User Input and Output”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 3 lessons in total.

What will I learn in “User Input and Output”?

Capture user input from the console and format output using puts and gets. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

How long does the “User Input and Output” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading and Writing Files
  2. User Input and Output
  3. Working with JSON and YAML
← Back to Ruby Academy