A string is one of the data type in Ruby. This is usually a bit of text that you wrap around double/single quotation marks which you want to display to someone.
In this blog post, I will attempt to explain how to reverse a string e.g. from “hello” to “olleh” without using Ruby’s reverse! method.
First let’s define a method like so:
My method is called reverse_string with a parameter string.
To test and see whether our method is working we can just return the string in our method, then call this method outside of our method using terminal:
You know the program is working when you see the output as “hello” in your terminal.
Now we can start defining our method. We can call a split method on the string in our reverse_string method and assign this to a variable → we can then call the variable instead of writing out the string and split method:
The parenthesis and quotation marks next to split tell the method how the string should be split up. When you do not add any spaces between quotation marks (“”), the string will be split up between individual characters as you are specifying that the characters themselves are the separators. However if you were to add a space to this such as (“ ”), this would mean that the split method would only split the string where there’s a space e.g.:
Note that the .split method automatically adds the split up string into an array.
We can then create an empty array so that later on we can use the push and pop methods.
Now what we want to do is to know the size of our string so that we can then repeat our push and pop method the same number of times as our string e.g. if my string’s is “hello”, the size is 5 and therefore the methods should be repeated 5 times to return the same number of characters.
We can now go ahead to define our push and pop method:
As the method is short, you don’t need to do ‘do’ and ‘end’ and use curly brackets instead. The « is the same as push. So what you are saying here is that from the array of split up strings, remove the last element in the array. Then the push method will append this to the reversed array. Below I have provided a more visual way of representing this:
The last we need to do is to join the string elements back together. We can use the join method which returns a string created by converting each element of the array to a string.
And that my friends, is how you reverse a string in Ruby without using the reverse! method…jk
There are plenty of other ways to do this but for now this is the simplest way that I understand. If anyone knows of other methods, please post the code in the comments below! I would love to know but please do explain what you are doing because I’m still a beginner and struggle to understand sometimes. Thanks!
- - -
This blog post has also been published on medium .