Example
| Before | After |
|---|---|
a = 1 result = foo(a)*123 if some_cond a = a + result else a = a - result end |
a = 1
result = foo(a)*FIXNUM
FIXNUM = 123
if some_cond
a = a + result
else a = a - result
end
|
class Person def greet() puts "Mr. #{'Bob'} greets you!" end end |
class Person BOB = 'Bob' def greet() puts "Mr. #{BOB} greets you!" end end |
The in-place refactorings are enabled in RubyMine by default. So, if you haven't changed this setting, the Extract Constant refactoring for Ruby is performed in-place, right in the editor:
- Place the cursor within the expression or declaration of a variable to be replaced by a constant.
-
Do one of the following:
- Press Ctrl+Alt+CCtrl+Alt+CCtrl+Alt+CCtrl+Alt+CCtrl+Alt+CCtrl+Alt+CAlt+Shift+CCtrl+Alt+CCtrl+Alt+CMeta Alt CMeta Alt CMeta Alt CMeta Alt C.
- Choose Refactor | Extract Constant in 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 EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter 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:
- Specify the name of the constant. Select the name from the list or type the name in the box with a red border.
-
To complete the refactoring, press TabTabTabTabTabTabTabTabTab or Meta+Close BracketTabTabTabTab or EnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnterEnter.
If you haven't completed the refactoring and want to cancel the changes you have made, press EscapeEscapeEscapeEscapeEscape, Escape or Ctrl+GEscapeEscapeEscapeEscapeEscapeEscapeEscapeEscape.
Note that sometimes you may need to press the corresponding key more than once.
If the Enable in place refactorings check box is cleared on the Editor settings, the Extract Constant refactoring is performed by means of the Extract Constant Dialog dialog box.

- In the Extract Constant Dialog dialog that opens, specify the name of the new constant.
- 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 constant.

