Share this post!

All posts by Jesus Castello

ruby atom plugins

Atom Tricks, Plugins & Shortcuts for Ruby Developers

If you are using Atom for Ruby development then you probably know that there are plugins (packages in Atom) that can improve your productivity with the editor.

But Atom’s package repository has thousands of packages!

Which ones should you be using?

And on top of that, what are some useful keyboard shortcuts you can use to work faster?

If you are an Atom user you are going to love this article because that’s exactly what I cover here!

Best Atom Packages

Atom packages add new functionality to the editor. They can be installed from the editor’s interface itself.

Open your settings (CTRL + ,) & click on the “Install” tab.


You can type in that search box to install the recommended packages.

Atom Runner

The first package I want to recommend is called “atom runner”.

This package allows you to run code directly inside your editor. The output will appear in a split window on the right (on the bottom if you have Atom 1.17 or newer).

It looks like this:


To install it simply search for its name on the package manager window & click “Install”. Then to launch it make sure your file type is set to Ruby & then press ALT + R (CTRL + R on Mac).

Note: If it doesn’t work try opening Atom from a terminal, instead of a menu or desktop icon.

If you want to increase the font size:

First open Atom’s style sheet file (with CTRL + ALT + P, type “osty”, then enter).

Then add this to the file:

Block Convert

You may want to convert between the do...end & { ... } block format. You can save yourself some work by using the “block convert” package.

With this package installed position your cursor inside a block, open the command-palette (CTRL + ALT + P) & search for “converter”, then select either “to do end” or “to curly brackets”.

This GIF demonstrates this package in action:


Toggle Quotes

You can easily switch between single & double quotes with the “toggle-quotes” package.

To use it position your cursor inside the pair of quotes you would like to switch then press CTRL + " (or CMD + " on a Mac).

Linter Ruby

A linter is a tool that points out errors in your code.

Mostly syntax errors, so don’t expect any miracles here 🙂

This helps you find these errors faster without even having to run your tests.

You can install the “linter-ruby” package if you want to enable linting in your editor.

Other Packages

Atom Keyboard Shortcuts

Everyone loves shortcuts!

Let me give you a table with some of my favorites:

Shortcut Description
CTRL + D Multi Selection
CTRL + F Search in current file
CTRL + P Search for files in current projects (quick open)
CTRL + B Switch between open files
CTRL + ALT + 7 Toggle comments for selected code
CTRL + UP / DOWN Move current line up & down
CTRL + Shift + K Delete current line

Here’s a GIF demonstrating the multi-select feature:


You can find even more shortcuts here:

Atom Snippets

Snippets allow you to create expandable templates to write code faster.

For example, you can type def & the enter key.

That will create a method template for you. It will also put your cursor on the method name so you can start typing right away.

After that you can press the “tab” key & it will place your cursor inside the method body.

Pretty useful, right?

Once you get used to it you will be doing this all the time.

Here’s a list of some useful built-in Ruby snippets:

Snippet Description
if if / end
ife if / else / end
def def / end
defs def / end
deft def test_ / end
cla class / end
mod module / end
ea each { … }
beg begin / rescue / end
Hash { … }
r attr_reader
w attr_writer
rw attr_accessor

There are also some ERB specific snippets, like = for <%= %> and - for <% %>.

Notice that your file type needs to be set to “Ruby” for these snippets to work. This happens automatically if you are editing a file with a .rb extension.

In addition, you can create your own snippets.

To do that just open your command palette (CTRL + ALT + P) & type “osni” then press enter.

A file will open where you can define your custom snippets.

A snippet looks like this:

Where the first string is the language for this snippet, then we have the name for this snippet (can be anything), and “prefix” is the actual word that will trigger the snippet, “body” is the code template.

If you have multiple snippets you don’t want to repeat the language part (.source.ruby), you just group everything under there, properly indented.

Notice the $1 here, that represents where the cursor is going to be positioned after the code snippet is inserted.

You can also have more than one of these cursor things.


When you press “tab” your cursor will go to $2, then $3, etc.

Think about what things you have to type often that are not already covered by built-in snippets, then write your own custom snippet for that.

Auto Indent

Sometimes your indentation can get out of whack & it’s a pain to fix it by hand…

Atom has you covered because it comes with an “Auto Indent” feature.

To use it select the code you want to indent, open the command palette (CTRL + ALT + P), then search for “auto”.

Built-In Git Integration

Atom includes Git integration since version 1.18.

To use it make sure the file you are editing is part of a project with an initialized git repository.

Note: You can also initialize the repository from inside the editor for new projects.

Then look at the lower right, it will say something like “3 files”.


Click on that & the Git panel will open where you can see the changes you made by clicking on each file.

Also you will be able to commit your changes by staging your changes, adding a commit message and clicking the “commit” button at the bottom of this panel.


In this article you learned how to become a more productive Ruby developer by installing useful packages, learning about keyboard shortcuts & defining custom snippets!

Like this? Don’t forget to share this article so more people can benefit from it 🙂

sinatra link shortener

Build Your Own Link Shortener App with Ruby & Sinatra

If you are looking for a little project to work on & practice your Ruby skills then you are in the right place.

Today I want to build a “link shortener” application together with you.

What’s a link shortener?

I’m sure you have seen them before…

These “short links” like:

When you visit this URL you get redirected to the original URL.

But how does that work?

The idea is to map this code, eKBRm2tTUF in the example, to the original URL on our database.

It’s a simple concept, but there are a few things we need to handle.


How do produce this code? How do you match it to the full URL?

So let’s get started!

First Steps

I’m going to be using Sinatra for this because it requires no setup at all, so we can get going right away.

And instead of a typical SQL database I’m also going to be doing something different.

Keep reading to find out what that is 🙂

Here’s the initial code:

This is a simple Sinatra app with 3 actions, or routes if you prefer.

We can visit our application & we will get the “Send a POST request” message.

How do we do that?

You can use a browser plugin like Postman.

But I like to use curl for this kind of thing.

It’s a handy tool that you should be able to install in any Operating System & it will allow you to interact with your Sinatra app.

Sending a POST Request

If you are following along you should have your Sinatra app running & curl installed on your machine.

Now you can send a POST request like this:

This should print the “New URL added” message on your terminal.


The next step is to map this URL to a shorter URL.

Let’s see how we can do that.

Mapping URLs

For the mapping you could do it using a random string, using a hash function or using some kind of encoding.

Here’s one way to do it:

In this case I’m going with a random string of 6 characters.

Watch the video if you want to see another way to do it.

This will give us the “short code” that we are going to use to map the original URL to the short URL.

What’s next?

We need to save this code somewhere so we can redirect users to the proper place.

Our “NoSQL” Database

For this project I just wanted to use a simple database.

And what’s simpler than just using files, right?

Ruby’s standard library includes this PStore class that will help us manage the data.

This is great for a small application like ours.

Here’s the code:

There are a few things going on here.

First, we have this store method to access our PStore object.

Then we have the save & read methods.

You will notice that PStore behaves like a hash. With the difference that every operation needs to happen inside a transaction block.

Now we can update our Sinatra app to use the ShortURL class:

And that’s it!

We now have a working link shortener.

Pro Tip: You can use the shotgun gem to auto-reload your Sinatra app when you make changes in your code.

There is still some work to do, like adding a form so we don’t have to use curl to submit a new URL.

Showing some message if the URL is not in the database.

And checking for duplicates, so we don’t overwrite other URLs.

I will leave those as homework for you 🙂


You learned how to build a Link Shortener application using Ruby, Sinatra & PStore! You also learned how to send a POST request using the curl http client.

Don’t forget to share this article with your friends so more people can learn.

ruby performance

Performance Improvements in Ruby 2.5

New Ruby versions keep improving & Ruby 2.5 is no different.

Ruby 2.5 is introducing some optimizations:

  • String interpolation will be around 72% faster when a large string is created
  • String#prepend will be around 42% faster if only one argument is given
  • Enumerable#sort_by, Enumerable#min_by & Enumerable#max_by will be about 50% faster

Let's see some benchmarks!

String Interpolation Performance

I took the example code in the commit message for this optimization:

I ran this benchmark with the following results.

Ruby 2.4.1:

Ruby 2.5:

As you can see the difference for large strings is pretty remarkable!

String#prepend Performance

The prepend method allows you to insert some text in front of an array.

Ruby 2.5 optimizes for the most common case, prepending only one string to another.

Here are the benchmark results.

Ruby 2.4.1 results:

Ruby 2.5 results:

That's a pretty good improvement!

Enumerable Performance Improvements

A few Enumerable methods are getting a performance upgrade.

This particular optimization works because it skips method dispatching for the <=> method.

As described on the commit message:

"Use OPTIMIZED_CMP() to compare the objects instead of <=> method dispatching for Fixnum/Float/String object."

Here are my benchmark results.

Ruby 2.4.2:

Ruby 2.5:

That's about a 50% improvement 🙂

Range#min & Range#max

I have two bonus performance optimizations for you!

One is about the Range#min & Range#max methods.

Here are the benchmarks:

Ruby 2.4.2

Ruby 2.5

Find the commit here.

Improved String#scan

According to the commit message this improves performance by 50% for a string pattern & 10% for a regex pattern.

Let’s look at the benchmarks!

Ruby 2.4.2

Ruby 2.5

Happy faster scanning!


You learned about the new optimizations coming in Ruby 2.5, to be released on December 25.

These optimizations are about string interpolation, Enumerable methods, the String#prepend method, the String#scan method & the Range#max / Range#mix methods.

I hope you enjoyed this post!

Don't forget to share the post on your favorite social networks 🙂

ruby array transpose

Turn Rows Into Columns With The Transpose Method

Imagine that you have a perfect grid, let’s say a 3×3 square, in the form of a multi-dimensional array.

And you want to take the rows & convert them into columns.

Why would you want to do that?

One use is for the classic game: tic-tac-toe.

You store your board as a grid. And to find a winning move you have to check the rows, columns & diagonals.

The problem is that if you are storing your grid as an array you only get direct access to the rows.

Columns The Hard Way

By “direct access” I mean that you can go over your array (with each, map, etc.) without having to use more array indexing than necessary.

Let’s see an example!

Here’s a grid:

Here’s a visual I made for you:

You could get the columns by referencing the indexes.

For example, the first column would be:

But the first row is just this:

How can we make working with columns as easy as working with rows?

Columns The Easy Way

The easy way to do this is by using the Array#transpose method.


Yes, that’s all you have to do!

Now you can get the first column like this:

As you can see knowing a lot of methods can save you a lot of work 🙂


I’m not going to explain the whole thing. I just want to show you how this method can apply to a real project.

To win a game of tic-tac-toe you need to fill a row, a column or a diagonal.

Here’s the code for checking rows:

And here’s is the code for columns:

Notice how the only difference is the transpose method!

Here’s the all_equal? method:

You can learn more about that each_cons method by reading this post on Enumerable methods.


You have learned about the transpose method.

Given a perfect grid, transpose allows you to transform the rows into columns for easy access.

If you enjoyed this post you may want to consider buying a copy of my book Ruby Deep Dive.

Array#zip Method

Let’s say that you want to compare two arrays, element by element so you can find the differences.

Or maybe you want to find the biggest number on every index, or you just want to merge a list of keys & a list of values to build a hash…

…the “hard way” to do this would look something like this.


That gets the job done, but it’s not very pretty, right?

In addition, you also have to stop & think for a minute to understand what’s going on.

Here’s is where Array#zip (also Enumerable#zip) comes into play!


Aha! …much better.

Do you agree?

If we are familiar with zip we will instantly understand what’s going on. And if we don’t, a quick read of the documentation (or even better, this article!) will explain it.

Another advantage of using zip is that we can easily chain it with other Enumerable methods.

Like for example, the count method:

This code returns a count of exactly how many elements are the same & in the same position.

Another trick you can use, like I mentioned before, is to merge a list of keys & values to build a hash.


Want another example?


Here it is:

This is a fancy implementation of the caesar cipher algorithm.


You learned about the Array#zip method, which allows you to “merge” two arrays & do some cool tricks with that 🙂

If you enjoyed this post don’t forget to share it on your favorite social network so more people can read it!