Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Array#join
(?::string? separator) -> ::String
join(separator = $,) → new_string
array.c
View on GitHub ↗
static VALUE
rb_ary_join_m(int argc, VALUE *argv, VALUE ary)
{
VALUE sep;
if (rb_check_arity(argc, 0, 1) == 0 || NIL_P(sep = argv[0])) {
sep = rb_output_fs;
if (!NIL_P(sep)) {
rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$, is set to non-nil value");
}
}
return rb_ary_join(ary, sep);
}
Returns the new string formed by joining the string-converted elements of self with the given separator (defaults to $,):
Example
$, # => nil %w[].join # => "" %w[foo].join # => "foo" a = %w[foo bar baz] # => ["foo", "bar", "baz"] a.join # => "foobarbaz" a.join('|') # => "foo|bar|baz" a.join(' :|: ') # => "foo :|: bar :|: baz"
Flattens and joins nested arrays:
Example
[:foo, [:bar, [:baz, :bat]]].join # => "foobarbazbat"
Related: see Methods for Converting.