NoComments is a Ruby gem designed to clean up .rb files by removing unnecessary comments, leaving your code clean and ready for deployment.
It removes:
- Single-line comments (e.g., # This is a comment)
- Block comments (e.g., =begin ... =end)
- Inline comments (e.g., puts 'Hello' # This is a comment)
It preserves:
- Shebangs (e.g., #!/usr/bin/env ruby)
- Magic comments (e.g., # frozen_string_literal: true)
- Tool-defined comments (e.g., # rubocop:disable all)
- Documentation comments (e.g., # @param id)
NoComments keeps Ruby code tidy by automatically removing unnecessary comments. It can be integrated into an MCP server to sanitize scripts before deployment, ensuring that the code published on the server is clean and production ready.
Example use cases:
- Auto-generated code – clear scaffolding comments produced by frameworks such as Rails.
- Projects with excessive comments – remove remarks that simply restate obvious code.
- CI/CD pipelines – incorporate NoComments as a step that enforces code cleanliness.
- Educational projects – clean files once learning phases are complete.
- Maintaining open source projects – keep contributions consistent and readable.
While NoComments streamlines your code, it is not a replacement for documentation comments or notes that are required for compliance.
- Code with valuable documentation – when comments explain complex algorithms or important business decisions.
- Regulated industries – if comments are mandatory for audit purposes.
- Rapidly changing projects – when comments capture ongoing discussions or decisions.
To install no_comments, add it to your Gemfile:
gem 'no_comments'Then execute:
bundle installOr install directly:
gem install no_commentsTo clean up comments from a .rb file or an entire directory, use the NoComments::Remover.clean method:
require 'no_comments'
# Clean a single file
NoComments::Remover.clean('path/to/your_file.rb')
# Clean all `.rb` files in a directory
NoComments::Remover.clean('path/to/your_directory')
# Clean all `.rb` files except selected ones or directories
NoComments::Remover.clean('path/to/your_directory', exclude: ['skip.rb', 'subdir'])
# Multiple files or directories can be provided, and blank entries are ignored.
# Absolute or relative paths work, and directories may have a trailing slash.
NoComments::Remover.clean('/abs/path/project', exclude: ['/abs/path/project/subdir/'])Audit mode allows you to preview the comments that would be removed without modifying the files. Use the audit: true flag with the clean method:
NoComments::Remover.clean('example.rb', audit: true)example.rb:
# This is a comment
def hello
puts 'Hello' # Another comment
endOutput:
File: example.rb
Line 1: # This is a comment
Line 3: # Another commentNoComments provides a Command Line Interface (CLI) for easy comment cleanup. The CLI supports the following options:
no_comments -p path/to/file.rb
no_comments -p path/to/directoryno_comments -p path/to/file.rb --audit
no_comments -p path/to/directory --auditno_comments -p path/to/file.rb --keep-doc-comments
no_comments -p path/to/directory --keep-doc-commentsno_comments -p path/to/directory --exclude file1.rb,subdir
# absolute paths and trailing slashes are supported. Blank entries are ignored.
no_comments -p /project --exclude /project/subdir/This gem uses RSpec for testing. To run tests:
- Install dependencies:
bundle install- Run the test suite:
bundle exec rspecTests are located in the spec directory and ensure that the gem behaves as expected, removing comments appropriately while preserving essential ones.
We welcome contributions! To contribute:
- Fork the repository.
- Create a new branch for your feature or fix:
git checkout -b feature-branch- Make your changes.
- Commit your changes with a descriptive message:
git commit -m 'Add new feature'- Push your branch:
git push origin feature-branch- Open a pull request on GitHub.
Please ensure your code follows the existing style and that all tests pass before submitting.
This gem is available as open source under the terms of the MIT License.
- Selective Cleaning:
- Allow users to clean all files in a directory except for specified ones.
NoComments is the perfect tool for keeping your codebase clean and focused, whether you're starting fresh or maintaining an existing project. By automating comment cleanup, you can focus on what matters: writing great code.