Archive

Archive for December, 2010

vim-ruby-refactoring – Extract Method

31 December, 2010 1 comment

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:
ExtractMethod_Before
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:
ExtractMethod_After
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

31 December, 2010 1 comment

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:
RenameInstanceVar_Before
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:
RenameInstanceVar_After
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

31 December, 2010 1 comment

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:
RenameVar_Before
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:
RenameVar_After
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

31 December, 2010 1 comment

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:
ExtractVar_Before
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:
ExtractVar_After
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

30 December, 2010 1 comment

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:
ExtractLet_Before 
Move the cursor on to the line you wish to extract
Hit your <leader-key> then type rel

After refactoring:
ExtractLet_After
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

30 December, 2010 1 comment

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:
ExtractConstant_Before 
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:
ExtractConstant_After
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

30 December, 2010 1 comment

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:

ConvertPostConditional_Before
Move the cursor onto the line which contains the post conditional expression
Hit your <leader-key> then type rcpc

After refactoring:

ConvertPostConditional_After
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

30 December, 2010 1 comment

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:

InlineTemp_Before
Move the cursor onto the temp variable (in this case base_price)
Hit your <leader-key> then type rit

After refactoring:

InlineTemp_After
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

30 December, 2010 1 comment

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:AddParameter_Before
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:

AddParameter_After
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

30 December, 2010 9 comments

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.

  1. Add Parameter
  2. Inline Temp
  3. Convert Post Conditional
  4. Extract Constant
  5. Extract to Let
  6. Extract Local Variable
  7. Rename Local Variable
  8. Rename Instance Variable
  9. 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.