Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

split

ruby

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?

like image 389
clockworkpc Avatar asked Sep 26 '17 22:09

clockworkpc


People also ask

What does ruby symbolize?

Rubies are often associated with wealth and prosperity. Many ancient crowns were decorated with rubies, because they represented good fortune and courage. The ruby's deep red color also has ties to love, passion, and raw emotion.

What is special about ruby?

Rubies are extremely strong, registering 9 on the Mohs scale of hardness. They are as resilient as sapphires and only slightly softer than diamonds. Rubies have been found all over the world, including in Myanmar (formerly Burma), Africa, Australia and the USA.

Why are rubies so rare?

Rubies are one of the rarest gemstones. The rarest rubies come from Burma (Myanmar), due to their high quality and exceptional color. Good quality rubies larger than one carat are also extremely rare—and expensive.

Are rubies expensive?

Fine-quality rubies are some of the most expensive gemstones, with record ruby prices over $1,000,000 per carat. However, rubies are also subjected to more treatments than almost any other gem. For this reason, ruby prices can range from a few hundred dollars a carat to thousands of dollars per carat.


2 Answers

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)

like image 67
Stefan Avatar answered Nov 16 '22 02:11

Stefan


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.

like image 42
SgtPepper Avatar answered Nov 16 '22 01:11

SgtPepper