Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created September 28, 2022 20:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/ba18ae4d7ff499c836999fe82094aac5 to your computer and use it in GitHub Desktop.
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
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