Empty Strings, Delimiters, and .split()oh my! A broad look on .split() in Ruby.

Jonathan T. Dean
2 min readMay 31, 2020
For you JS fanboys

In my pursuit of Software Engineering as a career, I’ve come to the conclusion I am addicted to learning. In this process of learning, I’ve come to a separate conclusion that I hate strings. With that out of the way here is my in depth look on .split() in Ruby.

Concerning the following let’s have a base case of

x = 'word'.split('')

Now let’s break this simple line down and explain what is happening behind the scenes.

I. <Variable : x> is the Container for the <String : ‘word’>.

II. The <String : ‘word’> has a length of 4, with each “index” having a character.

III. The .split(‘’), this is splitting the word by each character, whitespace included. This is known as an empty string, or a string with a length of 0. (Concerning Ruby)In fact by supplying an empty string, one may assume that this is the equivalent of supplying a NIL value, however that is not true. Run the following inside of whichever environment can run Ruby logic:

def empty_String!!''end

IV. The return value of the supplied <String : ‘’> is <Bool : true>.

Quick Breakdown of empty_String:

1. The func empty_String has one line of code, the string along with a double bang.

2. The Double bang converts the string to a boolean by reversing it’s Boolean Logic

I.E. Considering the following:

 !"Staten Island Turkey Gang" === false, 
!"Chicken" === false,
!'I am a phoenix' === false

3. Now the converted boolean value is reversed once more, since the converted value is <Bool : false> for <String : ‘’>. If we apply a bang to the converted string boolean we will get the real Boolean value of the supplied <String : ‘’> which is <Bool : true>.

4. This makes sense logically because if you do !false, you get <Bool : true>.

V. For each character split by the .split Method, the character is then placed inside of an array. Essentially the .split decides what the value of each element is. The characters in this case are transposed by the delimiter supplied to the .split parameter, which in this instance is the empty string delimiter.

Quick explanation of a delimiter, A delimiter is stating at which point a string should be considered Separate.

If your delimiter is a “,” then every line separated by a “,” will be considered Separate. If you use .split(‘,’) then you’re storing each string pair separated by a comma inside of an individual element.

I.E.

x = 'How, many elements, are there'.split(',')x.count = 3 # ["How", " many elements", "are there"]

**Notice the whitespaces are included inside the split elements**

Thank you for reading this far! My next post will be about (Ruby) .to_proc/enum(&) and some general use information on Primitives.

--

--

Jonathan T. Dean

Hi! I’m a Rust Developer living in N.Y.C.. Born and Raised in Brooklyn.