Split, strip, and join guides for strings
In this section, we are going to walk through how to use the split
, strip
, join
methods in Ruby. These methods will help us clean up strings and convert a string to an array so we can access each word as its own value.
Using the strip method
Let's start off by analyzing the strip
method. Imagine that the input you get from the user or from the database is poorly formatted and contains white spaces before and after the value. To clean the data up, we can use the strip
method. Consider this example:
str = " The quick brown fox jumped over the quick dog " p str.strip
When you run this code, the output is just the sentence without the white spaces before and after the words:

Using the split method
Now let's walk through the split
method. The split
method is a powerful tool that allows you to split a sentence into an array of words or characters. For example, type the following code:
str = "The quick brown fox jumped over the quick dog" p str.split
You...