This is a strategy I use to find unused code in ruby projects. These instructions are for macOS, but you only need access to the tools for this to work.
- Install unused following the instructions here
- Install universal-ctags. I use the brew instructions here
- Generate your tag file from your project root, including your library methods as well. I typically run
/usr/local/bin/ctags . $(bundle list --paths)
to add all of my library methods for the project. This has some other benefits, like allowing code-jumping in vimBonus: You can set this command up as a git hook to run every time you make changes to your code locally. I have an example of this here
- Once this command is done, you can look at the
tags
file in your repository root to make sure it's populated with data correctly (cat tags | wc -l
). For context, on an older project I have ~150,000 lines, on a newer project I have ~115,000 lines. In both cases, I have a healthy number of libraries included which are the bulk of the lines - Generate your list of unused methods, classes, and constants with
unused --ignore <path to gems/library> > out.txt
In my case, all of my gems are in
/Users/jay/.asdf/installs/*
; we ignore these paths because I don't care if gem X has an extra method
Inspect the out.txt
file to identify methods, classes, and constants you might be able to remove.
Caveats:
- This won't catch metaprogrammed or dynamically defined values, so be careful