clockworkpc
clockworkpc

Reputation: 688

Ruby #split("") vs #chars on a string

What is the difference between #split and #chars in Ruby when splitting a string?

"Hello, World".split("")
#=> ["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d"]

"Hello, World".chars
#=> ["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d"]

They both return an array, and they both include the blank space and punctuation.

Is there a scenario where one is preferable?

Upvotes: 14

Views: 13379

Answers (2)

Stefan
Stefan

Reputation: 114208

What is the difference between split and chars [...]?

string.chars parses the underlying bytes to returns the string's characters, whereas string.split('') uses a regular expression to achieve the same.

As a result, chars is faster and more robust. It even works if the string contains invalid characters:

"foo\x80bar".chars
#=> ["f", "o", "o", "\x80", "b", "a", "r"]

Whereas split fails if the string is malformed (because the regex engine can't handle it):

"foo\x80bar".split('')
#=> ArgumentError: invalid byte sequence in UTF-8

If I'm not mistaken, split('') is equivalent to split(//).

Is there a scenario where one is preferable?

split('') can be found in many tutorials. I assume this is because prior to Ruby 2.x, chars returned an enumerator. So in order to get an array you had to use two method calls:

string.chars.to_a

or a single call to: (which is also slightly shorter)

string.split('')

Nowadays, you'd use chars (or each_char for the pre-2.x behavior)

Upvotes: 18

SgtPepper
SgtPepper

Reputation: 468

You can use split for dividing strings into substrings based on a delimiter

For example:

"a/b-c".split('/') will return ["a", "b-c"]

chars instead returns an array of characters in string

"a/b-c".chars will return ["a", "/", "b", "-", "c"]

In conclusion, there are plenty of scenarios where one is more suitable than the other one.

Upvotes: 6

Related Questions