Notes on how to upgrade a legacy Ruby application Feb 21 2021

Upgrading any legacy application is a headache. You are trying to pay a technical debt of years in a couple of weeks or months. It isn’t easy, but it is also the best time to get things right for the next time you need to do an upgrade. In this short post, you’ll find some of my notes on upgrading a legacy Ruby application.

Let’s start with a step that sometimes we ignore, but it’s the most important, taking good notes.

Read More...

Basics of stderr and stdout on Ruby scripts Oct 23 2018

In our scripts, we present information to our users via the two standard output streams: standard output and standard error. In this post, I’ll explain why it is useful to distinguish between standard output and standard error, and share tips on how to interact with the streams when our script’s output is being redirected or used on a non-interactive shell.

The first part is the basics if you already know the basics you can skip to the last section to see an interesting example on how to take advantage of the streams.

Read More...

Exit codes for Ruby scripts Oct 15 2018

One of the most common mistakes I see in ruby scripts is the lack of good exit status, this prevents the script from being trusted in composition with other commands. The purpose of this post is to explain why exit codes are important and how to use them on our scripts.


Bash Beyond Basics Increase your efficiency and understanding of the shell

If you are interested in this topic you might enjoy my course Bash Byond Basics. This course helps you level up your bash skills. This is not a course on shell-scripting, is a course on improving your efficiency by showing you the features of bash that are seldom discussed and often ignored.

Read More...

Shell friendy Ruby scripts Sep 24 2018

When we build Ruby scripts, generally, we build them to be run independently but being able to compose them with other scripts makes them even better. How can we make our Ruby scripts be easy to compose? This is what this post is about, I will show you how to build your scripts so they can be composed or used independently.

Let’s see a basic Ruby script that receives a list of files and displays all lines capitalized.

Read More...

Ruby one-liner scripts Sep 17 2018

Ruby excels in its simplicity. It allows programmers to go from idea to implementation in a short time without much overhead. While many programmers have only heard of Ruby in relation to the web framework Rails, the scripting side of Ruby is very interesting and a rewarding use of the language. In this post, I will show you how to use Ruby to build one-liner scripts, and I will explain some useful flags when using Ruby scripts to process text.

Read More...

Using Thor and Ruby to build a CLI Sep 10 2018

Thor is a toolkit that can help us build command line interfaces(CLIs). You can find many tutorials on how to build a basic CLI using Thor. I want to explain the default behaviour of Thor and also when to use env to define the binary that will run your script.

We normally build custom scripts to automate tasks on our servers or local environments. Often these scripts require many flags or a big list of parameters. If the number of flags and parameters (from now on, options) are small then we won’t have a problem building the logic to handle them. However, when there are a number of options or we want to handle aliases, e.g. accepting the flag –delete or -d as the same operation, then things can become harder and more tedious to maintain.

Read More...

How to add RSpec to an existing Rails engine Aug 13 2018

Update 2018-08-27

I ran into a problem with Rails, it couldn’t find a database.yml, this was not because of a Rails missconfiguration but because of the binstub for rails had:

1
APP_PATH = File.expand_path('../../test/dummy/config/application', __FILE__)

And well we change it to use rspec, so it should be:

1
APP_PATH = File.expand_path('../../spec/dummy/config/application', __FILE__)

Adding RSpec to an existing Rails engine

If we already have a Rails engine and we wish to add RSpec to it, how would we go about it? The solution is not as straightforward as we might think, there are a few details always missing from tutorials or the guides, so I’ll try to share what I found and hopefully help someone that has the same issue.

Read More...

Rails update only one gem with bundler Jun 6 2018

When we use:

1
bundle update specific_gem

We expect to only update one gem the one we are telling bundler to update but that is not the case it updates the gem and all its dependencies, and if the dependencies have updates but the gem doesn’t the dependencies get updated no matter what.

so to avoid that use:

1
bundle update --source specific_gem

And that should do the trick.

Read More...

Using rbenv for multiple projects Feb 15 2018

I prefer to keep all my gems separated by project so I use rbenv-gemset you might want to do that too.

If you want to use a specific ruby version on a project do the following:

1
2
cd project
rbenv install 2.4.1 # you can pick any version `rbenv install -l`

then use that specif ruby-version on the project

1
rbenv local 2.4.1

This will create a .ruby-version file that contains the version you are using, to verify that you can check the version running:

Read More...