Automatic refactorings for Ruby.
While we try to make Reruby as safe as possible to use, we still consider it to be alpha. Please, commit your code before running any Reruby command.
Ruby is notoriously hard to refactor automatically, it's not just a dynamic language, but one where metaprogramming is not only allowed but even encouraged.
Reruby uses the excellent parser gem to transform Ruby code. This means that we mostly rely on syntactic information to perform the refactorings. Although not ideal, we've found that it works remarkably well for the operations that Reruby provides, and while it's true that some use cases are necessarily left out we firmly believe that a refactoring tool that works 90% of the time is better than no tool at all.
We are waiting for 0.1.0 in order to release on Rubygems. Meanwhile you can install by:
git clone https://github.com/dgsuarez/reruby.git
cd reruby
rake install
Note: Some of the refactorings will produce code that is not properly indented
and that may not follow your own styleguide. Reruby can optionally run
rubocop autofix on the changed/created
files after those operations by specifying the --rubocop-autofix
flag or
adding it to your configuration file (see below for more on this).
Rename classes or modules:
reruby rename_const 'Some::Const' 'NewConstName'
This will:
Some::Const
to Some::NewConstName
lib/some/const.rb
, app/models/some/const.rb
...)
and the test/spec fileRight now it won't, but should...
included
(it won't
recognize A::B
in include A; B
)B::A
and B::C::A
, and rename B::A
, every
usage of both will get replaced.Extract method refactoring:
reruby extract_method -l some/path.rb:2:10:4:9 my_new_method
This will:
some/path.rb
starting on line 2, column 10 until line 4
column 9.my_new_method
to the class where the code was defined,
with the required arguments.my_new_method
, again with the
required arguments.Right now it won't, but should...
Extract classes or modules defined in the same file as their parent to a file for each.
reruby explode_namespace MyClass
This will:
MyClass
defined in the same file as MyClass
MyClass
fileRight now it won't, but should...
Turn any instance variable read to a call to the reader method, adding the
attr_reader
declaration as well.
reruby instances_to_readers MyClass
This will:
attr_reader :var1, :var2...
@var1
, @var2
to var1
, var2
…Right now it won't, but should...
attr_reader
You can have per project configuration files in .reruby.yml
, or a global one
in ~/.reruby.yml
.
Here is a sample full configuration file:
paths:
exclude: # Regexes for paths to ignore when looking for code to transform
- ^vendor/
- ^coverage/
- ^log/
ruby_extensions: # Extensions for ruby files in your project
- .rb
- .rake
- .gemspec
- Rakefile
rubocop_autofix: true # Run rubocop -a over the files that have changed
extract_method:
keyword_arguments: true # Use keyword arguments when using extract method
All options can also be specified using the command line, run reruby --help
for more information.
Reruby reaches it's full potential when it's integrated in your text editor, here's a list of available plugins. Please let us know which integrations you would like to see, or if you know of an integration not available here.
If you'd like to create a new integration, our guidelines may help you get started.
After checking out the repo, run bin/setup
to install dependencies. Then,
run rake spec
to run the tests. You can also run bin/console
for an
interactive prompt that will allow you to experiment.
To run the executable while in development you can use bundle exec exe/reruby …
.
To install this gem onto your local machine, run bundle exec rake install
.
Bug reports and pull requests are welcome on GitHub at https://github.com/dgsuarez/reruby.
The gem is available as open source under the terms of the MIT License.