Ruby Development Tools Documentation - 0.9.0

Refactoring
Prev | Chapter 2. Concepts | Next

## Refactoring

RDT contains several automated refactorings for improving your code and making some tasks easier, like the generation of accessors. The refactorings are available through the menu bar as well as the context menu of the editor and the outline view. The following refactorings are available:

- Convert Local Variable to Field
- Encapsulate Field
- Extract Method
- Generate Accessors
- Generate Constructor using Fields
- Inline Class
- Inline Method
- Inline Temp
- Merge Class Parts
- Move Field
- Move Method
- Override Method
- Push Down Method
- Rename Class, Field, Method and Local Variable
- Split Local Variable

### Convert Local Variable to Field

This refactoring converts a local variable into a field. This will grant access to a variable that was only accessible inside a method or block to the whole class.

### Encapsulate Field

The Encapsulate Field refactoring encapsulates a field by hiding access to the field behind getter and setter methods. This is often used to enable external access, but can be useful to encapsulate a field internally within a class.

### Extract Method

Extract Method removes a block of statements and moves them into a new method that contains this functionality. The new method will be called from the location where the original statements appeared. Any local variables in scope of the extracted code that are used by the affected statements are passed to the new method as parameters. If one of those local variables is set inside the extracted block it will be returned from the new method as its return value.

### Generate Accessors

Generate Accessors can be used to generate getter and setter methods for fields of a class. The user can choose whether reader and / or writers are generated and if they should be inserted as methods or in the short form (using `attr_reader`, `attr_writer` or `attr_accessor`).

### Generate Constructor using Fields

The code generator Generate Constructor Using Fields creates a new constructor. The constructor will have a variable number of arguments, which can be selected from a list of the existing fields in the class. In the constructor's body, the class fields will be initialized with the values of the constructor's parameters.

### Inline Class

The Inline Class refactoring integrates the code from an existing class (which typically has few responsibilities) into another class. Typically the target class uses the inlined class.

### Inline Method

The Inline Method refactoring removes a method and replaces the call with its content. This might make sense if you have almost empty methods without much logic, perhaps after applying other refactorings. Generally having multiple methods with clear names is better than one big chunk of code.

### Inline Temp

The Inline Temp refactoring replaces all occurrences of a local variable with the value it was once assigned and removes the initial assignment. Usually this refactoring is used to simplify the existing code and lay the groundowrk for future refactorings.

### Merge Class Parts

The Merge Ruby Class Parts refactoring pulls multiple class declarations together and merges them into one single part. This is used to gather all instances of a re-opened class into a single area.

### Move Field

This refactoring moves a field from one class into another one. This is useful if you realize that the responsibility for a field is not in the owning class, but in another one. In this case, the field is better moved to the new class.

### Move Method

The Move Method refactoring behaves almost like the Move Field refactoring, except that it moves methods instead of fields. This is useful if you come across a method in a class that is not really responsible for the functionality provided by this method.

### Override Method

The Override Method code generator creates method bodies for a class. You can choose among the methods of the super class and those methods will be overridden in the class. The added methods will have the same signature as the one from the super class and call super.

### Push Down Method

The Push Down Method refactoring removes a method from the super class and pushes it into all its child classes.

### Rename Class, Field, Method and Local Variable

The rename refactorings can be used to change the name of various elements of your code.

### Split Local Variable

Programmers sometimes tend to assign a temporary variable several times (the fact that Ruby does not have typed references might misleadingly encourage this). This is not recommendable because this means that the temporary variable takes various responsibilities and thus can not be named properly. The Split Temporary Variables refactoring helps avoiding this by creating a new temporary variable for each responsibility.

Prev | Up | Next
Ruby Editor | Home | Console View