rubyguides

Kernel#print

print(*objects) -> nil

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.

That behavior is useful any time the line should stay open for a while, such as a prompt, a progress display, or a compact status message. print is simple by design, so the surrounding code controls when the line is finished.

Syntax

print obj
print obj1, obj2, obj3
print *objects

The syntax accepts a variable number of arguments through the splat operator. Each argument is converted with to_s and written sequentially to stdout without any separators or line breaks between them. This makes print a straightforward way to emit output when you want full control over the exact bytes that appear on the terminal.

Parameters

ParameterDescription
*objectsOne 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

The output stays on one line because print never adds its own newline. That makes the method easy to use in quick scripts where you want to see the output accumulate. This is the fundamental difference between print and puts: print gives you raw output control without any automatic formatting, while puts adds a newline after each argument. Understanding this distinction is key to choosing the right method for each output situation in your Ruby scripts. Once you internalize this single rule, most output decisions in your programs become straightforward and predictable.

with newline (combined with puts)

print "Enter your name: "
name = gets.chomp
puts "Hello, #{name}!"

This pattern shows the most common way to mix print and puts: print handles the prompt, and puts finishes the line after input is gathered.

This pattern shows the most common way to mix print and puts: print handles the prompt, and puts finishes the line after input is gathered. The two methods complement each other naturally, with print keeping the cursor in position and puts adding the line break when the interaction is complete.

multiple objects

print "Name: ", "Alice", " | Age: ", 30
# Name: Alice | Age: 30

Passing several objects at once keeps the formatting call short. Ruby converts each piece with to_s before writing it, so the method works well for mixed types.

Passing several objects at once keeps the formatting call short. Ruby converts each piece with to_s before writing it, so the method works well for mixed types. You can pass strings, numbers, symbols, and most other objects without worrying about type mismatches, because the conversion happens automatically inside the method call.

in loops

5.times do |i|
  print "#{i+1}... "
end
puts "Done!"
# 1... 2... 3... 4... 5... Done!

The loop example shows how print can produce a countdown or progress sequence in a single line. Each iteration prints one piece and the cursor stays in place, so the numbers accumulate left to right. This is a common pattern in command-line tools that want to show incremental progress without scrolling the terminal, and it pairs naturally with a final puts call to close the line cleanly.

progress indicators

10.times do |i|
  sleep 0.1
  print "\rProcessing: #{((i+1)*10)}%"
end
puts "\rCompleted!     "

The carriage return (\r) is what keeps the progress message on the same line. When the task is done, a final message can replace the in-place status and leave the terminal tidy. This technique works because the terminal interprets \r as “go back to column zero,” so each new print overwrites the previous one without scrolling the screen. It is a lightweight alternative to full terminal control libraries when the feedback is simple and the output format is predictable. These patterns are the building blocks of most interactive Ruby command-line tools.

common patterns

The common patterns section shows how print fits into real-world Ruby scripts. Each pattern uses the method for a slightly different purpose, but they all share the same core idea: keep the cursor on the line until the right moment to finish it. Once you understand how each pattern controls the line ending, you can mix and match them to handle most terminal output scenarios. These examples move from simple prompts to more structured uses like building strings and formatting output incrementally.

user input prompts

print "What is your name? "
name = gets
puts "Hello, #{name}!"

Prompts are often the clearest use of print, because the cursor should stay right after the question. The reply then appears on the same line and the next call can decide how to end the message. This idiom is so common that it appears in nearly every interactive Ruby script that reads user input from the terminal.

Building Strings

result = ""
[1, 2, 3].each do |num|
  result += print(num, " ") || ""
end
puts result

This example is deliberately small, but it shows an important detail: print returns nil, so it is a side-effect method rather than a value-producing one. That is why the example appends to result separately. Keep this in mind when chaining method calls, because print will always break the chain with a nil return value regardless of the arguments you pass to it.

formatted output without newlines

printf "Value: %.2f\n", 3.14159
# Value: 3.14

If you need formatting, printf and sprintf are a better fit. Use print when the formatting is already done and you only need the bytes on stdout. The choice between these output methods usually comes down to whether you already know how the output should look or whether the format needs to be decided at the last moment. Each method has a clear role, and mixing them intentionally gives you full control over terminal output.

differences from similar methods

MethodNewlineReturn ValueSpecial Behavior
printNonilNo newline
putsYesnilAdds newline after each object
pYesThe object(s)Uses inspect instead of to_s
printfNonilFormatted output (C-style)

The comparison table shows the core differences at a glance, but seeing the methods side by side in actual code makes the behavior more concrete. Once you remember that print never adds a newline and puts always does, most output decisions become obvious.

print "Hello"
print "World"
# HelloWorld

puts "Hello"
puts "World"
# Hello
# World

puts is the newline-friendly version, so it is the better default when each value should stand on its own line. print is the one to use when you are intentionally keeping the line open. The difference is easy to remember because puts stands for “put string” and always ends with a line break, while print writes exactly what you give it and nothing more.

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

The difference here is mostly about responsibility. print writes what it is given, while printf adds a formatting layer before the output is written.

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

print itself is simple, but the output stream still needs to be available. If stdout is closed or redirected in an unusual way, the method can fail when it tries to write.

using print for incremental output

print is a good match when the text should build up in place, such as prompts, progress updates, or any output that wants to stay on the same line. The lack of a trailing newline is the point, because it gives the caller control over where the line actually ends. That makes print useful in interactive scripts, but it also means the code should be deliberate about when a final puts or newline is added.

It can also help when the caller wants to combine several values into one line without extra formatting overhead. In that case the method acts like a small output brush rather than a full report generator. The surrounding code should still be clear about the line boundaries, since print will not add them for free. If a message needs a clean end point, the code should add it on purpose so the output stays predictable.

See Also