Skip to main content

How to Split a String in Ruby with split

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<pieces> = <text>.split(<separator>)
<pieces> = <text>.split(<separator>, <limit>)
<pieces> = <text>.split
PartWhat 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 argumentSplits 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:

main.rb, which prints with p the date 2026-11-28 split at the dash, split with a limit of 2, split at a slash, then a,b,,c and a,b,, split at the comma.

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​

Terminal
ruby main.rb
Output
["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"].

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 /, so split returned 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​

MistakeWhat happensDo 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 varyDouble spaces leave empty strings in the middleCall split with no argument
Expecting a limit to drop the restThe last piece keeps the rest of the stringSplit fully and take what you need

Troubleshooting​

SymptomLikely causeFix
An array with one element, the whole stringThe separator doesn't occur in the stringCheck the separator
nil when reading a pieceFewer pieces than the code expectedCheck 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​