Method: Array#join

Defined in:
array.c

#join(separator = $,) ⇒ Object

Returns the new string formed by joining the converted elements of self; for each element element:

  • Converts recursively using element.join(separator) if element is a kind_of?(Array).

  • Otherwise, converts using element.to_s.

With no argument given, joins using the output field separator, $,:

a = [:foo, 'bar', 2]
$, # => nil
a.join # => "foobar2"

With string argument separator given, joins using that separator:

a = [:foo, 'bar', 2]
a.join("\n") # => "foo\nbar\n2"

Joins recursively for nested arrays:

a = [:foo, [:bar, [:baz, :bat]]]
a.join # => "foobarbazbat"

Related: see Methods for Converting.



2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
# File 'array.c', line 2952

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