Skip to main content

How to Join an Array into a String in Ruby with join

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

In Ruby, array.join(', ') turns an array into one string, with ', ' between the items. Without a separator, the items run together.

Syntax​

Syntax
<array>.join(<separator>)
PartWhat it does
<array>The array to join. Each item is turned into a string first.
<separator>Optional. The text to put between the items, such as ', '. Without it, Ruby uses the global $,, which is nil unless a program sets it, so the items run together.
The resultA new string. The array itself doesn't change.

The Ruby documentation: join "Returns the new string formed by joining the converted elements of self", converting an item that's an array "recursively using element.join(separator)" and any other item "using element.to_s".

The Example​

The example joins a user's two roles, customer and beta-tester, three ways: with a comma and a space, with a bar, and with no separator at all. Then it calls join on roles that arrive as the single string 'customer' instead of an array.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which sets roles to customer and beta-tester and prints them joined with a comma and a space, with a bar, and with no separator.

main.rb
roles = ['customer', 'beta-tester']

puts roles.join(', ')
puts roles.join(' | ')
puts roles.join # no separator: the items run together

puts prints each joined string on its own line.

Step 2: Run It​

Terminal
ruby main.rb
Output
customer, beta-tester
customer | beta-tester
customerbeta-tester

The command is the same in zsh, bash and PowerShell.

Step 3: Call join on a String​

Replace main.rb with a version whose roles are a string, where an array was expected:

main.rb
roles = 'customer' # a string, where an array of roles was expected
puts roles.join(', ')
Terminal
ruby main.rb
Output
main.rb:2:in '<main>': undefined method 'join' for an instance of String (NoMethodError)

puts roles.join(', ')
^^^^^

A zsh terminal where ruby main.rb prints customer, beta-tester joined with a comma, then with a bar, then run together; for the changed file it stops with undefined method join for an instance of String (NoMethodError).

What Happened​

  • join(', ') put a comma and a space between the two roles, and join(' | ') put a bar with a space on each side.
  • join with no separator ran the items together: customerbeta-tester.
  • A string has no join method, so 'customer'.join(', ') stopped the script with undefined method 'join' for an instance of String (NoMethodError). Ruby printed the line that failed, with ^ marks under .join.

Common Mistakes​

MistakeWhat happensDo this instead
Putting an array straight into a string, as in "roles: #{roles}"You get the array as Ruby writes it in code: roles: ["customer", "beta-tester"]Join it first: "roles: #{roles.join(', ')}"
Printing an array with puts roles to get one lineputs prints each item on its own linePrint roles.join(', ')
Calling join on a value that may be a string or nilNoMethodErrorCheck is_a?(Array) first

Troubleshooting​

SymptomLikely causeFix
undefined method 'join' for an instance of String (NoMethodError)The value is a string, not an arrayCheck is_a?(Array) first, or use the string as it is
undefined method 'join' for nil (NoMethodError)The value is nil, for example a missing fieldFall back to an empty array: (roles || []).join(', ') gave ""
Empty places in the result, such as 10, , proAn item is nil, which becomes an empty stringDrop the nil items with compact first: [10, nil, :pro].compact.join(', ') gave 10, pro

FAQ​

What does join do with numbers, nil and nested arrays?​

It turns each item into a string with to_s, and it joins nested arrays too. In my check, [10, nil, :pro].join(', ') gave 10, , pro, because nil becomes an empty string, and ['a', ['b', ['c']]].join(', ') gave a, b, c. An empty array gave an empty string, "".

How do I turn the string back into an array?​

Split it on the same separator: 'customer, beta-tester'.split(', ') gave ["customer", "beta-tester"].

Conclusion​

join turns an array into one string, with the separator you choose between the items. Pass the separator yourself, and make sure the value really is an array, because a string or nil has no join.

Additional Resources​