End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Array#join
array.join →new_string
array.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 array elements after conversion. For each element element
-
Uses
element.to_sifelementis not akind_of?(Array). -
Uses recursive
element.join(separator)ifelementis akind_of?(Array).
With no argument, joins using the output field separator, $,:
Example
a = [:foo, 'bar', 2] $, # => nil a.join # => "foobar2"
With \string argument separator, joins using that separator:
Example
a = [:foo, 'bar', 2] a.join("\n") # => "foo\nbar\n2"
Joins recursively for nested Arrays:
Example
a = [:foo, [:bar, [:baz, :bat]]] a.join # => "foobarbazbat"