Building Custom Methods in Ruby

Methods

Today will be a simple walkthrough of what Ruby Methods are, how to create new custom ones and why we would even want to do something like that in the first place. To begin, let’s talk about what a method is and what it is not.

If you are coming to this tutorial from a background in Javascript just know that a Ruby Method is the same thing as a Javascript function – they are equivalent. In its purest form, a Method is a set of expressions that return a value when that method is invoked. For instance, you could have a method called times_10, and when you call that method on, say a variable equal to five (number = 5) you get 50 – number.times_10 returns 50.

A method is not necessarily built into the Ruby language, although some are like .reverse or .uppercase. These are examples of methods that are so commonly used in the process of programming that they were included into the Ruby language itself, for simplicities sake. If, however, you are writing a program and notice that you keep running the same command, over and over – it may be time to write yourself a new Ruby Method. This way of working adheres to the principles of DRY – Don’t Repeat Yourself.

It is much simpler to just call on a predefined method than it would be to repeat yourself over and over again, this is the hallmark of good code – it makes your program leaner, more efficient and ultimately more readable to the next set of eyes. Think of Methods as a factory – there are many moving parts in the factory, and they all take in some materials and work together to output a final product. Okay, let’s build some methods!

First off we need to understand the syntax of building a method in Ruby. To begin, the code block will start with a def and end with an end. After the def will be the name of the method itself, let’s call this one greeting. So we would have def greeting...end. In between the name of the method and the closing end, we will be putting all of our code which will be executed once we call on it.

Screenshot at Jun 14 12-50-14

This is just about the simplest version of a method I could possibly muster up. If we take a look, we will notice that the method is being invoked on line 8 and then this runs the code block that exists inside of the method – it prints to the screen, takes in a user input as a variable and then outputs using that variable.

This would be ideal if we had several people playing a game in the terminal and we needed to have each user input their name, instead of writing out this entire code block, we could simply build out the method and invoke it however many times we needed to.

Methods can also take in arguments though. The most common example would be for math equations. Let’s say we want to take in some variable and output something else, we could write a simple method. Let’s take a look at another example.

Screenshot at Jun 14 12-55-39.png

You can see that the method takes in an argument, num and then outputs (prints to the screen in this case) a new value that is equal to the arguments number multiplied by 10, hence the method name – times_ten. Super simple, then to invoke the method we simply call on it and input an argument.

A practical example of this would be something I used in a project I created called, Let’s Jam. I had a method in my User model that was called, is_student? and then it would check the User in question and based on the return, either true or false – it would display different information on the screen. It did this by referencing the value of a column on the users table for that user which denoted that user being either a student or teacher.

There are loads and loads of built in methods in the Ruby language, methods I use on a frequent basis. Methods like .to_s which will convert anything into a string, 4.to_s will output, "4". Then, there are things like, .downcase, .capitalize, .uppercase and many others.

It is important to leverage the power of your programming language to do more work for you and allow you to free up time to solve other problems. Remember, DRY code is good code!

Resources:

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s