DEV Community

Josh Lee
Josh Lee

Posted on • Updated on

How to Write Ruby Like a Pro – A Brief Style Guide

So you’ve been writing Ruby for a while. Maybe you’re familiar with control flow, data types, and the overall way to work with Ruby.

Before you start writing large programs or before working with others, it’s important that your code is consistent and sticks to a single style.

In this article, I’m going to give you some guidance on how to write Ruby that looks nice and that will be similar to how others write Ruby.

I got these guidelines while reading Eloquent Ruby by Russ Olsen. In his book, so if you want to know more about writing readable Ruby code, make sure to pick up his book.

Use spaces, not tabs

Make sure your code editor is set up to use spaces instead of tabs. And make sure that you are using two tabs for each level of indention.

Use # for comments

The hash sign (#) is used for comments in ruby. It’s also possible to write multi-line comments like so:

=begin
  this comment is
  two lines
=end
Enter fullscreen mode Exit fullscreen mode

But in the real world, most Rubyists use # even for multi-line comments.

# this comment is
# two lines
Enter fullscreen mode Exit fullscreen mode

Use snake_case for pretty much everything

Methods and variables should use snake_case. Constants should use SCREAMING_SNAKE_CASE.

Use parentheses most of the time

In Ruby, parentheses are optional. Here are two ways to call the same method in Ruby:

sum(2, 4)
sum 2, 4
Enter fullscreen mode Exit fullscreen mode

You should usually use parentheses when calling methods. However, some methods are often called without parentheses such as:

puts "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

It’s very common to not use parentheses in Ruby on Rails.

before_action :set_post, only: [:index, :create]
Enter fullscreen mode Exit fullscreen mode

You can also use parentheses in conditionals like so:

if (true)
 puts "true"
end
Enter fullscreen mode Exit fullscreen mode

Instead, you should omit the parentheses:

if true
  puts "true"
end
Enter fullscreen mode Exit fullscreen mode

It’s possible to write code-blocks in one line. Instead of this,

Use single-line code blocks when possible

Here's a normal code block

[1,2].each do |i|
  puts i
end
Enter fullscreen mode Exit fullscreen mode

you can write it as a single-line code-block:

[1,2].each { |i| puts i }
Enter fullscreen mode Exit fullscreen mode

Make use of if, unless, while, and until

In Ruby, you can write a conditional like so:

if true
  puts "true"
end
Enter fullscreen mode Exit fullscreen mode

But you can also write a single-line conditional like so:

puts "true" if true
Enter fullscreen mode Exit fullscreen mode

You can also take advantage of unless:

puts "true" unless dont_print
Enter fullscreen mode Exit fullscreen mode

While and until can also be used similarly:

puts "keep going" while has_gas
puts "keep going" until gas_is_empty
Enter fullscreen mode Exit fullscreen mode

The best way to learn how to write readable Ruby code is to read Ruby code written by more experienced developers. So, make sure you’re reading code by others often.

If you found this post helpful and want to get better at Ruby and all things web development, make sure to follow me on Twitter where I post helpful articles and tips all the time.

Discussion (0)