How to Join an Array into a String in Ruby with join
In Ruby, array.join(', ') turns an array into one string, with ', ' between the items. Without a separator, the items run together.
Syntax
<array>.join(<separator>)
| Part | What 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 result | A 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:

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
ruby main.rb
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:
roles = 'customer' # a string, where an array of roles was expected
puts roles.join(', ')
ruby main.rb
main.rb:2:in '<main>': undefined method 'join' for an instance of String (NoMethodError)
puts roles.join(', ')
^^^^^

What Happened
join(', ')put a comma and a space between the two roles, andjoin(' | ')put a bar with a space on each side.joinwith no separator ran the items together:customerbeta-tester.- A string has no
joinmethod, so'customer'.join(', ')stopped the script withundefined method 'join' for an instance of String (NoMethodError). Ruby printed the line that failed, with^marks under.join.
Common Mistakes
| Mistake | What happens | Do 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 line | puts prints each item on its own line | Print roles.join(', ') |
Calling join on a value that may be a string or nil | NoMethodError | Check is_a?(Array) first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
undefined method 'join' for an instance of String (NoMethodError) | The value is a string, not an array | Check 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 field | Fall back to an empty array: (roles || []).join(', ') gave "" |
Empty places in the result, such as 10, , pro | An item is nil, which becomes an empty string | Drop 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
- Array#join, in the Ruby documentation.
- String#split, in the Ruby documentation.
