const_stricter
The const_stricter is a Ruby library designed to identify unused code by analyzing constant dependencies in your project. There are two approaches to achieve this:
- Runtime Tracking: This method logs calls to classes and modules during runtime. While this approach provides reliable dependency map, it introduces runtime overhead and cannot detect references to undefined constants until the code is deployed to production.
- Static Code Parsing: This approach parses source files to identify constant usage. Although less precise in cases involving dynamically generated constant names, it allows you to detect errors and unused code before deployment, improving code quality early in the development cycle.
Gem const_stricter uses Static Code Parsing via prism to help developers maintain cleaner codebases by identifying and removing unused constants efficiently.
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add const_stricter --group "development"If bundler is not being used to manage dependencies, install the gem by executing:
gem install const_stricterUsage
Run rake task from the root folder of your application:
rake const_stricter:lintAs a result, you'll see something like following (run bin/lint_dummy from gem root):

The section Dynamic constants shows strings that can only be calculated in runtime.
The pseudo-code in Missed constants means that the constant HTTP is missing (can't be evaluated) in context of Traceable.
You can specify which files need to be checked by setting the glob parameter in the rake task ({app,lib}/**/*.rb by default):
rake const_stricter:lint[lib/*.rb]Limitations
The const_stricter can't find constants whose names are set using meta-programming. Examples in tests
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/corp-gp/const_stricter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the const_stricter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.