Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Array#join

(?::string? separator) -> ::String
join(separator = $,) → new_string

Returns the new string formed by joining the string-converted elements of self with the given separator (defaults to $,):

Example
$,                  # => nil
%w[].join           # => ""
%w[foo].join        # => "foo"
a = %w[foo bar baz] # => ["foo", "bar", "baz"]
a.join              # => "foobarbaz"
a.join('|')         # => "foo|bar|baz"
a.join(' :|: ')     # => "foo :|: bar :|: baz"

Flattens and joins nested arrays:

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

Related: see Methods for Converting.

See also #intersection #keep_if #intersect? #last also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0