The Split Method and the Power of Punctuation
This is my first blog, and it is going to be about the mysterious ways of the split method. I had the chance to learn more about this method while working on a lab called the OO Cash Register Lab during the first module of Flatiron School’s Software Engineering program at Access Labs in Brooklyn, NY.* So, I will write about some of my misadventures while completing that lab and what I learned about the split method along the way.
The prompt asked us to do the following:

One of the methods I was supposed to write would take parameters of a “title” of an item, the quantity of the item, and the price of it. It was supposed to update the total price of all items combined in the shopping cart and it was also supposed to add new items to the @items array to keep track of what was in the shopping cart.
This is the beginning of the code to get you oriented:

This is the first method I wrote, for adding an item to the cart:

And below is the small part of the above that this blog is about. Dealing with quantity was what was most difficult for me at the time. The more stuff one has, the harder it can be to keep track of it.
First, here is what worked, and then I will go through some of the things that didn’t work. The binding.pry is included to show you how the array @items ends up looking under different conditions.

When I call binding.pry on this, this is what I see:

So, the tests are looking to see whether we can handle different quantities of an item, in this case books. (Anyone with towers of books surrounding them in their apartment can tell you that it is important to try to keep them organized, even if more often than not this is a losing battle!)
Three books were added to the cart, and we see that reflected in the @items array. But it took me a long time to get to that point, so I will back up to show you some of the things that could (and did) go wrong.
First, I explored just multiplying the title by the quantity, like so (the working code is commented out):

But this is how that turned out:

Instead of listing three new books in the cart, I have one big triple book. (Large volumes are not easy to carry around with you to read on the train, so this is a problem!) So, I knew with the help of binding.pry that I needed to find a way to split up the three books that did not currently want to be apart. This would also have to be the case for any quantity of items, not just for the test case of books. It occurred to me thanks to Learn.co’s previous lessons that there was a method for that, the split method. Below is Ruby Doc’s definition of the split method.

https://ruby-doc.org/core-2.4.0/String.html#method-i-split
Ruby Doc gives a number of examples of ways to split a string, and I will discuss a few of them. The problem that I ran into initially after suspecting that I should somehow put the split method to use was that there was nothing to split [“bookbookbook”] by, no punctuation marks and no white space, for example. Just calling .split on the array did nothing (yes, I am sure I tried that!). Using “k” as a delimiter, which you can try for fun like so:

did this:

That might have been good for Halloween last week, but it is not exactly what I was looking for! Also, we might want to add items to the cart that do not have a “k” in them, and in that case this would also not do anything we want it to do.
Ruby Doc tells us that we can split along white space, like this, if there is white space to serve as a delimiter:

Or , it tells us, we could split along each letter, like this:

to give us this:

But that is a little like buying a book by the chapter, too many volumes instead of too few.
Another example of a delimiter that Ruby Doc gives us that I like, in spite of how ineffectual it would appear to be for the OO Cash Register Lab is:

Here is a link to that song, in case you are interested. Thank you Ruby Doc. https://www.youtube.com/watch?v=IQNBQI3UDag
This was all enough to get me to go back to try to interpolate my way out of the problem before again coming back to kneel at the shrine of the split method. So, I tried to interpolate a comma between the items like this:

and got this:

It looked like I was getting closer, so I edited the above to also include a space after the comma, like this:

to get this:

Things were looking good, and I was feeling clever; but there was still an extra comma and space at the end of the string, and I still had one string of books instead of three separate strings. This is what brought me back to the split method.
The split method separates your string into smaller strings and adds the commas for you while leaving off the unwanted comma at the end. I knew that the split method was the answer, even though it was giving me so many problems.
What I ended up realizing was that the split method speaks the language of punctuation. There are other delimiters, but punctuation is one thing it likes to work with. Since I am a beginner at software engineering, I don’t know first-hand whether computers are stupid, as some people say. But I think that there is something smart about the focus that the split method has on punctuation.
Learning how to punctuate properly can be a difficult thing to master. It is something that as a writer and speaker, one can also overthink and in doing so miss the mark. When punctuation does not serve primarily to convey a message clearly, it can serve a stylistic purpose. It can arrest people’s attention, make them unengaged, or make them wonder about something that is left hanging. Punctuation helps us to organize our thoughts. It gives them a certain speed(the urgency of the exclamation point) or forces us to take our time (commas might gather speed, but they can also make us take our time depending on the context). So, I like that the split method speaks the language of punctuation, even though it took me awhile to appreciate this, and even though there are still things about the method that are a mystery to me.
So, that is how I got to the final result:

giving me:

I didn’t have to add a space after the comma in the “title” interpolation, because the split method separates the strings in its resulting array according to the conventions of English grammar, or at least the grammar of arrays, with a comma and a space. Also, if I had added a space it would have actually just created other problems.
I had to use flatten!, because without it I had a nested array, like this:

This happened without the flatten! method being used on the array, because the @items variable was set equal to an empty array. Then, the split method turned the string into an array of strings, which is what I was pushing into the empty @items array. Flatten! collapses the nested arrays into one array that can later on be iterated over to display the contents of the shopping cart or do other things.
In spite of enjoying discovering that the split method speaks the language of punctuation, it turns out that I could have also achieved the same result by just adding a whitespace to the interpolation and then using white space as the delimiter, like this:

Since it doesn’t matter whether you use a punctuation mark or white space to serve as a delimiter, you can rely on your own preferences (or your own whims at the moment) for either punctuated or blank space.
*Since this is my first blog and it is about programming a cash register, I am going to have a “Hi, mom!” moment and give a shout out to my dad, who has spent the last forty years fixing cash registers.
