vim-ruby-refactoring – Extract Method
This post is part of a series which documents the vim-ruby-refactoring plugin.
IMPORTANT: As well as installing the vim-ruby-refactoring plugin, you must also install the matchit.vim plugin for this refactoring to work.
Extract Method
Extracts a selection into a method and places it above the current method.
The refactoring: http://www.refactoring.com/catalog/extractMethod.html
Example
Before refactoring:
Visually select lines you wish to extract
Hit your <leader-key> then type rem
You will now see a prompt to enter the new method name:
Method name: print_details
After refactoring:
A new method print_details has been added above the print_owing method containing the contents of the selected lines.
rem is the default binding for this refactoring, think Refactor Extract Method.
vim-ruby-refactoring – Rename Instance Variable
This post is part of a series which documents the vim-ruby-refactoring plugin.
IMPORTANT: As well as installing the vim-ruby-refactoring plugin, you must also install the matchit.vim plugin for this refactoring to work.
Rename Instance Variable
Renames the selected instance variable.
Example
Before refactoring:
Visually select the instance variable you wish to rename
Hit your <leader-key> then type rriv
You will now see a prompt to enter the new variable name:
Rename to: new_name
After refactoring:
The instance variable @name has been renamed to @new_name in both locations within the class.
rriv is the default binding for this refactoring, think Refactor Rename Instance Variable.
vim-ruby-refactoring – Rename Local Variable
This post is part of a series which documents the vim-ruby-refactoring plugin.
IMPORTANT: As well as installing the vim-ruby-refactoring plugin, you must also install the matchit.vim plugin for this refactoring to work.
Rename Local Variable
Renames the selected local variable.
Example
Before refactoring:
Visually select the local variable you wish to rename
Hit your <leader-key> then type rrlv
You will now see a prompt to enter the new variable name:
Rename to: is_mac_os
After refactoring:
The local variable mac_os has been renamed to is_mac_os in both locations within the method.
rrlv is the default binding for this refactoring, think Refactor Rename Local Variable.
vim-ruby-refactoring – Extract Local Variable
This post is part of a series which documents the vim-ruby-refactoring plugin.
Extract Local Variable
Extracts a visual selection into a local variable.
The refactoring: http://www.refactoring.com/catalog/introduceExplainingVariable.html
Example
Before refactoring:
Visually select the value you wish to extract
Hit your <leader-key> then type relv
You will now see a prompt to enter the variable name:
Variable name: mac_os
After refactoring:
The value platform.upcase.include?(‘MAC’) has been extracted into a local variable mac_os.
relv is the default binding for this refactoring, think Refactor Extract Local Variable.
vim-ruby-refactoring – Extract to Let
This post is part of a series which documents the vim-ruby-refactoring plugin.
Extract to Let
This is an RSpec specific refactoring which will extract an initialisation line and create a let method for you.
Example
Before refactoring:
Move the cursor on to the line you wish to extract
Hit your <leader-key> then type rel
After refactoring:
The let method is created above the it block, using the initialisation line – account = Account.new.
rel is the default binding for this refactoring, think Refactor Extract Let.
vim-ruby-refactoring – Extract Constant
This post is part of a series which documents the vim-ruby-refactoring plugin.
Extract Constant
Extracts a selection into a constant which is placed at the top of the current module or class.
The refactoring: http://www.refactoring.com/catalog/replaceMagicNumberWithSymbolicConstant.html
Example
Before refactoring:
Visually select the value you wish to extract
Hit your <leader-key> then type rec
You will now see a prompt to enter the constant name:
Constant name: Gravitational_Constant
After refactoring:
The value 9.81 has been extracted into a constant GRAVITATIONAL_CONSTANT which is placed at the top of the module.
rec is the default binding for this refactoring, think Refactor Extract Constant.
vim-ruby-refactoring – Convert Post Conditional
This post is part of a series which documents the vim-ruby-refactoring plugin.
Convert Post Conditional
Converts a post conditional expression to a conditional expression.
Example
Before refactoring:
Move the cursor onto the line which contains the post conditional expression
Hit your <leader-key> then type rcpc
After refactoring:
The post conditional expression is split across 3 lines and converted into a standard if expression.
rcpc is the default binding for this refactoring, think Refactor Convert Post Conditional.
vim-ruby-refactoring – Inline Temp
This post is part of a series which documents the vim-ruby-refactoring plugin.
Inline Temp
Replaces a temporary variable with a direct call to the method or formula.
The refactoring: http://www.refactoring.com/catalog/inlineTemp.html
Example
Before refactoring:
Move the cursor onto the temp variable (in this case base_price)
Hit your <leader-key> then type rit
After refactoring:
The temp variable base_price has been replace with a direct call to the method and we’ve saved a line of code.
rit is the default binding for this refactoring, think Refactor Inline Temp.
vim-ruby-refactoring – Add Parameter
This post is part of a series which documents the vim-ruby-refactoring plugin.
Add Parameter
Simply adds a parameter (or many separated with commas) to a method.
The refactoring: http://www.refactoring.com/catalog/addParameter.html
Example
Before refactoring:
Move your cursor onto the method where you wish to add the parameter
Hit your <leader-key> then type rap
You will now see a prompt to enter the parameter name:
Parameter name: date
After refactoring:
The contact method now has a date parameter added.
rap is the default binding for this refactoring, think Refactor Add Parameter.
The default <leader-key> in vim is the ‘\’ key but you can remap this to any key you like.
Using vim to write ruby code? Check out this excellent refactoring plugin
I’ve written a series of posts documenting the excellent vim plugin vim-ruby-refactoring. As you may have guessed from the name, its a Ruby refactoring plugin for vim.
- Add Parameter
- Inline Temp
- Convert Post Conditional
- Extract Constant
- Extract to Let
- Extract Local Variable
- Rename Local Variable
- Rename Instance Variable
- Extract Method
IMPORTANT: When installing the vim-ruby-refactoring plugin, make sure you also install the matchit.vim plugin otherwise some of these refactoring’s will not work.