Why should you use Ruby for your next command-line tool?

One day, I needed to write a command-line tool. It was more complicated than copying a couple of shell commands to a file with .sh extension, but at the same time it was too simple to make a full Java or Scala application out of it.

Even though I work mostly with Scala and Typescript, I’ve chosen to do it in Ruby because it’s good for that very purpose – to quickly get your stuff done.

Here is why:

TDD is first-class citizen

Ruby is a dynamic language. That means, that the type of the object is not checked before executing an operation. Objects can be modified at runtime, methods can be added dynamically, etc.

This sounds like hell for those coming from languages like Java or Scala, but it’s very helpful if you want to do a TDD approach.

Because there isn’t any compiler to do some checks, proper testing is very important in Ruby. Because of that, there are a lot of tools, which help with that. RSpec lets us write tests with a surprisingly small amount of lines. Nesting lets us reuse the setup between cases, keeping our tests testing only one thing at a time. This makes not only writing, but also reading tests easier.

  context('when the rectangle has size 2 x 3') do
    subject { Rectangle.new(2, 3) }

    it 'should return correct area' do
      expect(subject.area).to eq(6)

    it 'should return a correct circumference' do
      expect(subject.circumference).to eq(10)

There are lots of other libraries, which we can use alongside RSpec. One example, which I’ve never seen anywhere else is the vcr gem. It can be used to record calls to external APIs during tests execution and use cached data on the following runs instead of making the same calls over and over again. The funny thing is, that such things wouldn’t be possible (at least not that easily) if Ruby wasn’t a dynamic language.

Tools and community support

Because of Rails popularity, there are Gems (Ruby libraries) for almost everything. Using the work someone once done instead of reinventing the wheel can greatly speed up the development. Also, most of the problems you may face are probably already solved and explained in a Stack Overflow question.

Object-oriented and High-level API

Ruby’s APIs are simple and with a quite high level of abstraction. For making a command-line tool this is great because we can focus on getting the job done, abstracting the details away.

require 'csv'

lines = CSV.readlines("filename.csv")

Operations on collections are also syntactically nicer and more familiar to those I know from Scala or Kotlin. I realize, that it’s just a matter of taste, but for me, the intention is clearer this way.

> [1, 2, 3, 4, 5, 6, 7].select { |number| number > 3 }

 => [4, 5, 6, 7]
> ["lorem", "ipsum", "dolor"].each_with_index do |word, index|
>   puts "#{index+1}. #{word}"
> end

1. lorem
2. ipsum
3. dolor

 => ["lorem", "ipsum", "dolor"]

Interpreted language

I’ve mentioned, that Ruby is not a compiled language, thus there is no compiler to check for syntax errors. But what you get in return is instant feedback. You don’t have to wait for your application to compile to see the results.

This may not sound that convincing – after all, we are developing a simple tool, so the compilation times shouldn’t be that long after all. But on the other hand, you could gain on the faster feedback loop. If your tests run almost instantly, running them automatically each time you change your code suddenly doesn’t sound that insane (check out the guard gem).


Don’t get me wrong – I don’t want to say, that you should out of the sudden use Ruby for everything. In my current job, we use Scala and I find it an excellent choice for a product developed by a larger team. However for that specific scenario – a simple command-line tool – it might be worth to at least consider Ruby.

I realize that I may be biased because I’ve been working with the Rails framework in the past. If you are used to other languages such as Python or Go, it may be a good idea to choose them and be productive with tools you already know. However, once in a while, it’s good to expand your horizons and discover what our options do you have.

Leave a Reply

Your email address will not be published. Required fields are marked *