Kernel#puts
puts(*objects) -> nil Returns:
nil · Added in v1.0 · Updated March 13, 2026 · Kernel Methods output io console
puts is a Kernel method that writes one or more objects to standard output (stdout), appending a newline character after each object. It’s the most common way to output text in Ruby scripts and is often used for debugging and displaying information to users.
Syntax
puts obj
puts obj1, obj2, obj3
puts *objects
Parameters
| Parameter | Description |
|---|---|
| *objects | One or more objects to output. Each object is converted to a string using to_s and followed by a newline. |
Examples
Basic Usage
puts "Hello, World!"
# Hello, World!
puts "Line 1"
puts "Line 2"
# Line 1
# Line 2
Multiple Objects
puts "Name:", "Alice", "Age:", 30
# Name:
# Alice
# Age:
# 30
Arrays
arr = ["apple", "banana", "cherry"]
puts arr
# apple
# banana
# cherry
nil and Booleans
puts nil # (outputs empty line)
puts true # true
puts false # false
Common Patterns
Debugging
def calculate_total(prices)
total = prices.sum
puts "Debug: prices = #{prices.inspect}, total = #{total}"
total
end
Formatted Output
name = "Bob"
age = 25
puts "Name: %s, Age: %d" % [name, age]
# Name: Bob, Age: 25
Writing to stderr
puts always writes to stdout. For stderr, use $stderr.puts:
$stderr.puts "Error: something went wrong"
Differences from Similar Methods
| Method | Newline | Return Value | Special Behavior |
|---|---|---|---|
puts | Yes | nil | Adds newline after each object |
print | No | nil | No newline |
p | Yes | The object(s) | Uses inspect instead of to_s |
printf | No | nil | Formatted output (C-style) |
print vs puts
print "Hello"
print "World"
# HelloWorld
puts "Hello"
puts "World"
# Hello
# World
p vs puts
puts "hello\n" # hello (interprets escape)
p "hello\n" # "hello\n" (shows raw string with newline literal)