← Reference · Nestor G Pestelos Jr · Print this page

Programming Languages · Ruby

Ruby String Splitting

Reference entry · last updated September 21, 2026

Ruby string splitting is String#split, which divides a string at a separator and returns an Array of substrings. The separator is either a literal String or a Regexp, and those two behave differently. An optional limit caps the number of fields. The result distinguishes "no separator was found" from "the separator was found and the field is empty".[1]

First principles and definitions

Two pattern types

The pattern decides the algorithm. A String pattern is matched literally: every character is ordinary, so regex metacharacters lose their meaning. A Regexp pattern is matched as a regular expression, and the string is divided wherever it matches.[1]

"THX1138".split(/\d+/, 2)      # => ["THX", ""]
"THX1138".split('\d+', 2)      # => ["THX1138"]

The first call splits on one or more digits. The second looks for the literal characters backslash, d, plus, finds none, and returns the whole string as one element. Single quotes keep the backslash; a double-quoted "\d+" drops it to "d+".

The whitespace default

With no argument, the pattern is nil, and the value of $; is used. Because $; is nil by default, the string is split on whitespace as if a single space had been passed. That form ignores leading and trailing whitespace and treats a run of whitespace as one separator.[1]

" now  the  time ".split        # => ["now", "the", "time"]
"a\nb".split("\n")               # => ["a", "b"]
"".split                         # => []

An empty string returns an empty Array, because there are no fields to split.

The limit argument

The optional second argument caps how many fields are returned. It also controls trailing empty fields.[1]

LimitEffect
omittedtrailing empty fields are suppressed
positive nat most n fields plus any captured groups; the last field keeps the unsplit remainder
1the whole string as a one-element Array
negativeno field limit, and trailing empty fields are kept
"a,b,c".split(",", 2)     # => ["a", "b,c"]
"a,b,,".split(",")        # => ["a", "b"]
"a,b,,".split(",", -1)    # => ["a", "b", "", ""]

One exception: when the pattern contains a capture group, the captured groups are inserted into the result as well, and they do not count toward the limit.[1]

"a,b,c".split(/(,)/, 2)  # => ["a", ",", "b,c"]

The limit is a separate argument to split, not part of the pattern. Writing it inside the regex, as in split(/\s*=\s*, 2/), makes it pattern text and also leaves the regex unterminated if the closing slash is lost.

Results and edge cases

With a limit of 2, the Array length carries information that a multiple assignment hides. A one-element result means no separator matched, so a second variable receives nil. A two-element result with an empty second string means the separator matched and the field after it is empty.[1]

line = "badline"
key, value = line.split(/=/, 2)   # key "badline", value nil

line = "a="
key, value = line.split(/=/, 2)   # key "a", value ""

That reading depends on the limit. With the default limit, trailing empty fields are suppressed, so "a=".split(/=/) returns ["a"] even though the separator matched. The limit of 2 keeps the empty field and makes the distinction visible.

Those are different cases: nil is "there was no separator", while "" is "there was a separator with nothing after it". A guard written as value.nil? || value.empty? conflates them and drops valid empty fields.

Choosing a method

References

  1. ^ Ruby 3.3 core documentation, "String," split. Free full text: docs.ruby-lang.org/en/3.3/String.html
  2. ^ Ruby 3.3 core documentation, "String," partition. Free full text: docs.ruby-lang.org/en/3.3/String.html