Created
September 28, 2022 20:32
-
-
Save havenwood/ba18ae4d7ff499c836999fe82094aac5 to your computer and use it in GitHub Desktop.
A little script to run RuboCop on a Ruby String from within Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubocop' | |
require 'tempfile' | |
class InlineRunner | |
DEFAULT = RuboCop::Runner.new({}, RuboCop::ConfigStore.new) | |
def initialize(runner: DEFAULT, filename: 'runner.rb') | |
@runner = runner | |
@file = Tempfile.new(filename) | |
end | |
def run(code) | |
@file.write(code) | |
@file.rewind | |
@runner.run([@file.path]) | |
ensure | |
@file.close | |
@file.unlink | |
end | |
end | |
runner = InlineRunner.new | |
example_code = 'class Wombat end' | |
runner.run example_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment