Array#join

arr.join(separator=nil) -> string
Returns: string · Updated March 13, 2026 · Array Methods
arrays join strings conversion

.join converts an array elements into a single string by concatenating their string representations with a separator between each element. This is the inverse operation of String#split, and it is one of the most common ways to build output strings from collections.

Syntax

array.join           # join with empty string (default)
array.join("")      # join with empty string explicitly  
array.join(",")     # join with comma
array.join("\n")    # join with newline
array.join(", ")    # join with comma and space

Parameters

ParameterTypeDefaultDescription
separatorString or nilnilThe string to insert between each element. If nil, uses the global $, variable (which defaults to nil, producing an empty string).

Examples

Basic usage

fruits = ["apple", "banana", "cherry"]

fruits.join
# => "applebananacherry"

fruits.join(", ")
# => "apple, banana, cherry"

fruits.join(" | ")
# => "apple | banana | cherry"

Without a separator argument, .join produces a concatenated string with no delimiter between elements.

Working with numbers

numbers = [1, 2, 3, 4, 5]

numbers.join("-")
# => "1-2-3-4-5"

# Numbers are converted to strings automatically
[10, 20, 30].join(", ")
# => "10, 20, 30"

.join automatically converts non-string elements using to_s, so you can work with arrays containing mixed types.

Nested arrays

nested = ["a", ["b", ["c", "d"]], "e"]

nested.join
# => "abcde"

nested.join(", ")
# => "a, b, c, d, e"

.join handles nested arrays recursively. It calls .join on any array element, so deeply nested arrays flatten automatically into the output string.

Building CSV-like output

rows = [
  ["Name", "Age", "City"],
  ["Alice", "30", "NYC"],
  ["Bob", "25", "LA"]
]

rows.map { |row| row.join(",") }.join("\n")
# => Name,Age,City
# => Alice,30,NYC
# => Bob,25,LA

This pattern is useful for generating CSV data from arrays of arrays.

Common Patterns

Joining with newlines for display

items = ["Item 1", "Item 2", "Item 3"]

puts items.join("\n")
# Output:
# Item 1
# Item 2
# Item 3

Creating URL query strings

params = ["page=1", "limit=10", "sort=name"]

"?" + params.join("&")
# => "?page=1&limit=10&sort=name"

Flattening and joining in one step

matrix = [[1, 2], [3, 4], [5, 6]]

matrix.flatten.join(",")
# => "1,2,3,4,5,6"

This is equivalent to .join on nested arrays since .join already handles recursion.

Using with map for custom formatting

users = [{name: "Alice", age: 30}, {name: "Bob", age: 25}]

users.map { |u| "\#{u[:name]} (\#{u[:age]})" }.join(", ")
# => "Alice (30), Bob (25)"

Errors

.join is forgiving - it works with arrays of any types and handles nil elements gracefully:

[nil, "a", nil, "b"].join(",")
# => ",a,,b"

Nil elements produce empty strings in the output.

See Also