Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
String#%
(::array[untyped] positional_args) -> ::String
(::hash[::Symbol, untyped] named_args) -> ::String
(untyped arg) -> ::String
string % object → new_string
string.c
View on GitHub ↗
static VALUE
rb_str_format_m(VALUE str, VALUE arg)
{
VALUE tmp = rb_check_array_type(arg);
if (!NIL_P(tmp)) {
return rb_str_format(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), str);
}
return rb_str_format(1, &arg, str);
}
Returns the result of formatting object into the format specification self (see Kernel#sprintf for formatting details):
Example
"%05d" % 123 # => "00123"
If self contains multiple substitutions, object must be an Array or Hash containing the values to be substituted:
Example
"%-5s: %016x" % [ "ID", self.object_id ] # => "ID : 00002b054ec93168" "foo = %{foo}" % {foo: 'bar'} # => "foo = bar" "foo = %{foo}, baz = %{baz}" % {foo: 'bar', baz: 'bat'} # => "foo = bar, baz = bat"