Ruby Methods— Cheat Sheet

A great thing about being a developer nowadays is that you can just type your questions on google and usually find the solution you were looking for.

However, as a beginner, I often found myself looking for the same methods over and over on the internet. Whether I didn’t remember the syntax or I forgot which method I should use. Developer’s time is precious, so there must be a better way to handle this.

My advice: a Cheat Sheet. Whenever you use a method more than once but you are not sure you will remember it, this is a perfect candidate for your Cheat Sheet

Below is a list of methods I found useful so far:

1. Delete a word or a sentence from a string

Let’s say I am returning the name of a file in my repository. The string contains the filename “test.rb” but also its directory “a/b/c/”.

Let’s have a look at a way to remove it:

In the above example, I first entered the string that I want to clean up: “a/b/c/test.rb”. Then, I added “.gsub” which is working as a find and replace method. It takes two arguments: something to find (‘a/b/c/’)and something to replace it with(which is nothing, written with quotes with no space in between). The method will return the string “test.rb”.

2. Repeat the same action a specific amount of times

This second method is a time saver if you would like to repeat the same action for a specific amount of time

First, specify how many times the action should be repeated (in our example “3”). Add “.times” and then enter the action to be repeated between curly brackets. In my example, the method will return 3 times “Hello, world!”

3. Check if an array includes a specific value

When you have an array of names, you might want to check if a specific name is included.

In the above, I’m calling the “include?” method against my array of names to check if “Henry” is included in this array. “Henry” is included so the method returns “true”. If there was no one called “Henry” in my array, it would have returned “false”

4. Return a random element

If you want to return a random element for your array, there is a method too!

In the above example, I first use an array and add “.sample” at the end
The method will then return a random element of the array. Try running it several times, it should return a different value.

5. Sum an array of Integers/Floats

Let’s say we want to know the total of an array of prices

To sum an array of Integers or/and Floats, add “.sum” at the end of the array. It will return the total value of the elements of the array.

6. Remove nil from an array

If you have a messy array full of “nil” values, there is a method to clean it up for you.

Simply add “.compact” at the end of your array and it will return a new array without any “nil”.

Conclusion

This is obviously a non-exhaustive list and there are many other useful methods I don’t know of yet that will make their way to my Cheat Sheet.

If you find this article useful, I encourage you to write your own Cheat Sheet with your own words, showing the method, what it returns with a brief explanation of what you can use this method for.

JavaScript Mid-Level Developer, I write blogs to learn and share what I learn.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store