Method: Kernel#printf

Defined in:
io.c

#printf(format_string, *objects) ⇒ nil #printf(io, format_string, *objects) ⇒ nil

Equivalent to:

io.write(sprintf(format_string, *objects))

For details on format_string, see Specifications[rdoc-ref:format_specifications.rdoc].

With the single argument format_string, formats objects into the string, then writes the formatted string to $stdout:

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:

printf($stderr, '%4.4d %10s %2.2f', 24, 24, 24.0)

Output (on $stderr):

0024         24 24.00# => nil

With no arguments, does nothing.

Overloads:

  • #printf(format_string, *objects) ⇒ nil

    Returns:

    • (nil)
  • #printf(io, format_string, *objects) ⇒ nil

    Returns:

    • (nil)


8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
# File 'io.c', line 8647

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;
}