Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns the new String formed by joining the array elements after conversion. For each element element
  • Uses element.to_s if element is not a kind_of?(Array).
  • Uses recursive element.join(separator) if element is a kind_of?(Array).
With no argument, joins using the output field separator, $,:
a = [:foo, 'bar', 2]
$, # => nil
a.join # => "foobar2"
With string argument separator, 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"
 
               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);
}
            

Was this page useful?