Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Kernel#print

print(*objects) → nil

Equivalent to $stdout.print(*objects), this method is the straightforward way to write to $stdout.

Writes the given objects to $stdout; returns nil. Appends the output record separator $OUTPUT_RECORD_SEPARATOR $\), if it is not nil.

With argument objects given, for each object:

  • Converts via its method to_s if not a string.

  • Writes to stdout.

  • If not the last object, writes the output field separator $OUTPUT_FIELD_SEPARATOR ($, if it is not nil.

With default separators:

Example
objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero']
$OUTPUT_RECORD_SEPARATOR
$OUTPUT_FIELD_SEPARATOR
print(*objects)

Output:

nil
nil
00.00/10+0izerozero

With specified separators:

Example
$OUTPUT_RECORD_SEPARATOR = "\n"
$OUTPUT_FIELD_SEPARATOR = ','
print(*objects)

Output:

0,0.0,0/1,0+0i,zero,zero

With no argument given, writes the content of $_ (which is usually the most recent user input):

Example
gets  # Sets $_ to the most recent user input.
print # Prints $_.
See also #pretty_inspect #printf #pp #proc also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0