Skip to content

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

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, $,:

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"
See also #intersection #keep_if #intersect? #last also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0