5 Tips to Instantly Write Better Code in Ruby for Beginners

Takehiro Mouri
Learning How to Code
4 min readFeb 21, 2016

Now that you’ve learned the basic syntax and gained a basic understanding of ruby works, you want to learn how to write better and more concise code. Here are 5 quick tips picked up from a fantastic book called Eloquent Ruby that you can start using right now to make your code more readable.

Don’t write comments for the wrong reasons

Adding comments, especially in a more complex app can be useful. You have to make sure that you provide comments so that other people can read and understand your code. You also have to make sure that you can understand the code yourself after a while.

On the other hand, you don’t want to get in the mindset of, “Oh, this code is pretty messy but I’m going to add comments so it’ll be fine.” Adding comments to make badly written code somewhat comprehensible is an extremely bad reason for adding comments. In general, your ruby code should be clear enough so that it explains itself. Hence the quote from the book, “Good code is like a good joke: it needs no explanation.” (pg. 8 — Eloquent Ruby)

Ternary Operators

Ever write code like this?

if current_user.gender == "male"
true
else
false
end

This makes perfect sense, except we can make it much shorter with ternary operators. Ternary operators work like such:

if_this_is_a_true_value ? then_the_result_is_this : else_it_is_this

So in this example, we can re-write the code like this:

current_user.gender == “male” ? true : false

Method Name Conventions: Using ? and !

When you are using ruby, you may have noticed that a lot of the built in methods use ? and !. For example .nil? or .flatten! use these conventions. ? and ! aren’t special symbols in method names, but rather are just naming conventions in the ruby community.

?s are used when the method returns a true or false boolean. So for example instead of this:

def is_male 
current_user.gender == “male” ? true : false
end
current_user.is_male

You want to write add a ? to the method like this:

def is_male? 
current_user.gender == “male” ? true : false
end
current_user.is_male?

You can see here that current_user.is_male? makes a little more sense than current_user.is_male, since we are essentially asking a yes or no question.

Now let’s take a look at the ! operator in methods. The ! operator when used in a method infers that the method is potentially dangerous and could change the state of the variable that the method is called upon. Here’s an example:

a = [1, 2, 3, 4]print a.reverseprint a # returns [1, 2, 3, 4]

What we see here is that we expect .reverse to actually modify the array a. However, we see that it doesn’t modify the actual array, but it just returns the reversed array. Now what if we change the reverse method to reverse!?

a = [1, 2, 3, 4]print a.reverse!print a # returns [4, 3, 2, 1]

Now we see that reverse! has actually modified the actual array. It’s important to know the differences between methods with and without the bang operator.

Using Unless and Until

Sometimes you find yourself in a situation like this:

if !current_user.is_male? 
puts “I’m a woman”
end

or alternatively

if not current_user.is_male? 
puts “I’m a woman”
end

Instead, it is often times more intuitive to use unless.

unless current_user.is_male? puts “I’m a woman” 
end

This sounds closer to English and is far more readable.

Same thing with while loops. Instead of writing this kind of code:

while !fruit.is_ripe? 
puts “This fruit isn’t ripe yet.”
end

You could make the code much easier to mentally process by using until and rewriting it like this:

until fruit.is_ripe? 
puts “This fruit isn’t ripe yet.”
end

It takes less mental energy to read and understand code if we use unless and until in these kinds of situations.

One Line Statements

Let’s say we have code written like this:

if current_user.is_male? 
puts “I’m a male”
end

Instead of using multiple lines, we can rewrite this code in one line like this:

puts “I’m a male” if current_user.is_male?

This applies to things like unless as well:

puts “I’m a woman” unless current_user.is_male?

When writing these one-liners, make sure that the line isn’t too long. The point here is to make the code more readable, so you don’t want to defeat the purpose by cramming a bunch of code into one line just because you can.

Just implementing these simple techniques will make you a better ruby programmer. Ruby offers many elegant solutions like these and learning them are really worth the time.

Happy coding :)

I write about coding in my personal blog. Every now and then, I share coding articles that I’ve found or written that you might find to be useful or interesting. Subscribe here.

If you found the post useful, please recommend and follow :)

--

--