Kernel#print
print(*objects) -> nil Returns:
nil · Added in v1.0 · Updated March 13, 2026 · Kernel Methods output io console
print is a Kernel method that writes one or more objects to standard output (stdout) without appending a newline character. Unlike puts, it keeps the cursor on the same line, allowing you to build up output incrementally.
Syntax
print obj
print obj1, obj2, obj3
print *objects
Parameters
| Parameter | Description |
|---|---|
| *objects | One or more objects to output. Each object is converted to a string using to_s and written without a trailing newline. |
Examples
Basic Usage
print "Hello"
print "World"
# HelloWorld
With Newline (combined with puts)
print "Enter your name: "
name = gets.chomp
puts "Hello, #{name}!"
Multiple Objects
print "Name: ", "Alice", " | Age: ", 30
# Name: Alice | Age: 30
In Loops
5.times do |i|
print "#{i+1}... "
end
puts "Done!"
# 1... 2... 3... 4... 5... Done!
Progress Indicators
10.times do |i|
sleep 0.1
print "\rProcessing: #{((i+1)*10)}%"
end
puts "\rCompleted! "
Common Patterns
User Input Prompts
print "What is your name? "
name = gets
puts "Hello, #{name}!"
Building Strings
result = ""
[1, 2, 3].each do |num|
result += print(num, " ") || ""
end
puts result
Formatted Output Without Newlines
printf "Value: %.2f\n", 3.14159
# Value: 3.14
Differences from Similar Methods
| Method | Newline | Return Value | Special Behavior |
|---|---|---|---|
print | No | nil | No newline |
puts | Yes | nil | Adds newline after each object |
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
print vs printf
print "Pi is approximately ", 3.14159, "\n"
# Pi is approximately 3.14159
printf "Pi is approximately %.3f\n", 3.14159
# Pi is approximately 3.141
Errors
print itself rarely raises errors. However, if stdout is closed or not available, you may get an IOError:
$stdout.close
print "hello" # => IOError: closed stream