Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
String#%
(::array[untyped] positional_args) -> ::String
(::hash[::Symbol, untyped] named_args) -> ::String
(untyped arg) -> ::String
self % 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)) {
VALUE result = rb_str_format_ary(RARRAY_LENINT(tmp), RARRAY_CONST_PTR(tmp), str, tmp);
RB_GC_GUARD(tmp);
return result;
}
return rb_str_format(1, &arg, str);
}
Returns the result of formatting object into the format specifications contained in self (see Format Specifications):
Example
'%05d' % 123 # => "00123"
If self contains multiple format specifications, object must be an array or hash containing the objects to be formatted:
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"
Related: see Converting to New String.