← All languages
Ruby method of the day
Random

join

Combine all elements into a string with a separator.

Description

The join method returns a string created by concatenating all elements of the array, with an optional separator inserted between each pair. Each element is converted to a string using to_s before joining.

If no separator is given, the elements are joined without any separator (equivalent to an empty string separator). Nested arrays are also recursively joined.

This method is commonly used to build output strings from arrays of parts, such as creating CSV lines, constructing paths, or generating formatted lists.

Arguments

NameDescriptionOptional
separator The string to insert between elements. Defaults to empty string. Yes

Example

["a", "b", "c"].join(", ")   # "a, b, c"
[1, 2, 3].join("-")          # "1-2-3"
["hello", "world"].join(" ") # "hello world"

Reference