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 converted elements of self; for each element element:
-
Converts recursively using
element.join(separator)ifelementis akind_of?(Array). -
Otherwise, converts using
element.to_s.
With no argument given, joins using the output field separator, $,:
Example
a = [:foo, 'bar', 2] $, # => nil a.join # => "foobar2"
With string argument separator given, 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"
Related: see Methods for Converting.