Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO#print
(*untyped objects) -> nil
print(*objects) → nil
io.c
View on GitHub ↗
VALUE
rb_io_print(int argc, const VALUE *argv, VALUE out)
{
int i;
VALUE line;
/* if no argument given, print `$_' */
if (argc == 0) {
argc = 1;
line = rb_lastline_get();
argv = &line;
}
if (argc > 1 && !NIL_P(rb_output_fs)) {
rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$, is set to non-nil value");
}
for (i=0; i<argc; i++) {
if (!NIL_P(rb_output_fs) && i>0) {
rb_io_write(out, rb_output_fs);
}
rb_io_write(out, argv[i]);
}
if (argc > 0 && !NIL_P(rb_output_rs)) {
rb_io_write(out, rb_output_rs);
}
return Qnil;
}
Writes the given objects to the stream; returns nil. Appends the output record separator $OUTPUT_RECORD_SEPARATOR ($\), if it is not nil. See Line IO.
With argument objects given, for each object:
-
Converts via its method
to_sif not a string. -
Writes to the stream.
-
If not the last object, writes the output field separator
$OUTPUT_FIELD_SEPARATOR($,) if it is notnil.
With default separators:
Example
f = File.open('t.tmp', 'w+') objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero'] p $OUTPUT_RECORD_SEPARATOR p $OUTPUT_FIELD_SEPARATOR f.print(*objects) f.rewind p f.read f.close
Output:
Example
nil nil "00.00/10+0izerozero"
With specified separators:
Example
$\ = "\n" $, = ',' f.rewind f.print(*objects) f.rewind p f.read
Output:
Example
"0,0.0,0/1,0+0i,zero,zero\n"
With no argument given, writes the content of $_ (which is usually the most recent user input):
Example
f = File.open('t.tmp', 'w+') gets # Sets $_ to the most recent user input. f.print f.close