How to Split a String in Ruby with split
String#split cuts a string into pieces wherever a separator appears and returns the pieces as an array. '2026-11-28'.split('-') gives the year, the month and the day. Ruby quietly drops empty pieces at the end, which is worth knowing.
Syntax
<pieces> = <text>.split(<separator>)
<pieces> = <text>.split(<separator>, <limit>)
<pieces> = <text>.split
| Part | What it does |
|---|---|
<separator> | The text to cut at. It doesn't appear in the pieces. |
<limit> | Optional. A positive number returns at most that many pieces, the last one holding the rest. -1 keeps empty pieces at the end. |
.split with no argument | Splits on runs of whitespace and ignores whitespace at both ends. |
The Ruby documentation for String#split: it "Creates an array of substrings by splitting self at each occurrence of the given field separator field_sep."
The Example
The example splits a date at its dashes, splits it again into at most two pieces, splits it at a separator it doesn't contain, and splits two comma lists with empty spots in them. p prints each array with its quotes, so empty strings are easy to see.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

date = '2026-11-28'
p date.split('-')
p date.split('-', 2)
p date.split('/')
p 'a,b,,c'.split(',')
p 'a,b,,'.split(',')
Step 2: Run It
ruby main.rb
["2026", "11", "28"]
["2026", "11-28"]
["2026-11-28"]
["a", "b", "", "c"]
["a", "b"]
![A zsh terminal where ruby main.rb prints ["2026", "11", "28"], ["2026", "11-28"], ["2026-11-28"], ["a", "b", "", "c"] and ["a", "b"].](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/ruby/2026/rb-string-split-zsh.webp)
The command is the same in zsh, bash and PowerShell.
What Happened
split('-')cut the date at both dashes.- With a limit of
2, the second piece kept the rest of the date,11-28. - The date has no
/, sosplitreturned an array with the whole string as its only piece. - In
'a,b,,c', the empty piece between the two commas stayed. - In
'a,b,,', the empty pieces at the end were dropped, so only["a", "b"]came back.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting empty pieces at the end to stay | 'a,b,,'.split(',') gives ["a", "b"] | Pass -1: 'a,b,,'.split(',', -1) gave ["a", "b", "", ""] |
Splitting on ' ' when the spaces vary | Double spaces leave empty strings in the middle | Call split with no argument |
| Expecting a limit to drop the rest | The last piece keeps the rest of the string | Split fully and take what you need |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| An array with one element, the whole string | The separator doesn't occur in the string | Check the separator |
nil when reading a piece | Fewer pieces than the code expected | Check pieces.size first |
FAQ
What's the difference between p and puts for an array?
p shows the array as Ruby writes it, with brackets and quotes. puts prints each element on a line of its own, which hides empty strings.
Can the separator be a pattern?
Yes. split also takes a regular expression, such as split(/[,;]/) to cut at commas and semicolons.
Conclusion
text.split(separator) returns the pieces between the separators as an array, drops empty pieces at the end unless you pass -1, and splits on any whitespace when called with no argument.
Additional Resources
- String#split, in the Ruby documentation.
