My Ruby Kata Template
Recently I’ve been aiming to do more katas, both in my personal time, and at work in our Deliberate Practice group.
I noticed that whenever I was starting on a new kata, I was doing the same basic setup repeatedly before being able to write a first test. I also felt I was missing some of the configuration that my other ‘real’ projects benefited from.
This led me to create ruby-kata-template. By clicking on Use this template, I get a new blank project in seconds, configured to my preferences.
I don’t expect my preferences to match everyone else’s, so I encourage you to fork it and customize to your own ideal template.
Features
Starting Point
There is an empty Project (project.rb
) and a corresponding test (project_test.rb
). I expect the first step in many katas will be to rename these.
Directory Structure
I’ve followed the standard Ruby convention of the implementation being in lib/
and the tests in test/
Minitest
Although Minitest is already provided as a Bundled gem with Ruby, it’s included in the Gemfile
to ensure we’re on the latest version. (I’ve used RSpec extensively in the past, but my current preference is Minitest).
ActiveSupport::TestCase
ActiveSupport::TestCase
is part of Rails but here we are using it independently. It allows you to write ‘declarative’ tests, e.g. test "it works" do...
rather than def test_it_works
, which I feel is more readable.
Rake Task
It’s common on Ruby apps to have rake
or rake test
, so we provide that. It has support for both MiniTest naming conventions (test_*.rb
and *_test.rb
)
Standard
Standard is an opinionated RuboCop configuration.
For me, the real value of Standard (or RuboCop) is only apparent when you have two things configured in your editor:
- Immediate feedback (your editor highlights problems as you code)
- Auto-formatting (you can fix issues with one single shortcut, or automatically when the file is saved).
There are various ways to set up this, but the template uses rubocop-lsp
.
rubocop-lsp
rubocop-lsp is a gem which implements the Language Server Protocol. It allows for a close integration between your editor with RuboCop. It avoids the overhead of starting RuboCop, meaning linting or auto-correction is near-instant.
To use it, you’ll also need an plugin or extension for your editor. The template is set up to recommend the VS Code Shopify.rubocop-lsp extension.
Solargraph
Solargraph adds IDE-like experience for your editor. It provides helpful features such as a content-aware autocompletion, and documentation for the Ruby language.
However, I often run into problems with it, so I was on the fence about including it.
It’s not as powerful as Sorbet, so it’s unable to infer types unless they are a Ruby primitive (String, Array, etc.)
And more
Take a look at the README to learn what else is included.