Extract Field refactoring declares a new field and initializes it with the selected expression. The original expression is replaced with the usage of the field.
Let's extract the
anotherClass.intValue(); variable into a field. As a result, RubyMine changes the selected variable name to
number and declares it as the
private int number field.
Extracting a field in-place
To extract a field in-place
The in-place refactorings are enabled in RubyMine by default. So, if you haven't changed this setting, the Introduce Field refactorings are performed in-place, right in the editor:
- Place the cursor within the expression or declaration of a variable to be replaced by a field.
- Do one of the following:
- Press Ctrl+Alt+F.
- Choose Refactor | Introduce Field on the main menu, or on the context menu.
- If more than one expression is detected for the current cursor position, the Expressions list appears. If this is the case, select the required expression. To do that, click the expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press Enter to select it.
- If more than one occurrence of the expression is found within the class, specify whether you wish to replace only the selected occurrence, or all the found occurrences with the new constant:
- If relevant, specify where the new field will be initialized - in the current method, or in a class constructor.
- Specify the name of the field. Select the name from the list or type the name in the box with a red border.
- To complete the refactoring, press Tab or Enter.
If you haven't completed the refactoring and want to cancel the changes you have made, press Escape.
Note that sometimes you may need to press the corresponding key more than once.
Extracting a field using the dialog box
To extract a field using the dialog box
If the Enable in place refactorings checkbox is cleared on the Editor settings, the Extract Field refactoring is performed by means of the introduce field.
- In the editor, select the expression or variable to be replaced with a field, or just place the cursor within such an expression or variable declaration.
- In the main menu, or the context menu of the selection, choose , or press Ctrl+Alt+F.
- In the Expressions pop-up menu, select the expression to be replaced. Note that RubyMine highlights the selected expression in the editor.
- In the dialog that opens, specify the type and name of the new field.
- To automatically replace all occurrences of the selected expression (if it is found more than once),select the option Replace all occurrences.
- Click OK to create the field.