End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Kernel#printf
printf(format_string, *objects) → nil
printf(io, format_string, *objects) → nil
io.c
View on GitHub ↗
static VALUE
rb_f_printf(int argc, VALUE *argv, VALUE _)
{
VALUE out;
if (argc == 0) return Qnil;
if (RB_TYPE_P(argv[0], T_STRING)) {
out = rb_ractor_stdout();
}
else {
out = argv[0];
argv++;
argc--;
}
rb_io_write(out, rb_f_sprintf(argc, argv));
return Qnil;
}
Equivalent to:
Example
io.write(sprintf(format_string, *objects))
For details on format_string, see Format Specifications.
With the single argument format_string, formats objects into the string, then writes the formatted string to $stdout:
Example
printf('%4.4d %10s %2.2f', 24, 24, 24.0)
Output (on $stdout):
0024 24 24.00#
With arguments io and format_string, formats objects into the string, then writes the formatted string to io:
Example
printf($stderr, '%4.4d %10s %2.2f', 24, 24, 24.0)
Output (on $stderr):
0024 24 24.00# => nil
With no arguments, does nothing.