RubyMine 6.3.0 Web Help

The Extract Parameter refactoring is used to add a new parameter to a function declaration and to update the function calls accordingly.

Example

Before After
                     class MyClass
    def my_method()
        String a = return_string();
        ...
    end
                     def return_string()
        s = a_string.toLowerCase()
        ...
    end
                     end
                  
                     class MyClass
    def my_method()
        a = return_string(a_string_to_lower_case)
        ...
    end
                     def return_string(a_string_to_lower_case)
        s = a_string_to_lower_case
        ...
    end
                     end
                  
Extracting a parameter in Ruby in-place

The in-place refactorings are enabled in RubyMine by default. So, if you haven't changed this setting, the Extract Parameter refactorings for Ruby are performed in-place, right in the editor:

  1. In the editor, place the cursor within the expression to be replaced by a parameter.
  2. Do one of the following:
    • Press Ctrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PAlt+Meta+PAlt Command PAlt Command PAlt Command PAlt Command P.
    • Choose Refactor | Extract | Parameter in the main menu.
    • Choose Refactor | Extract | Parameter from the context menu.
  3. 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.

    ruby_IntroduceParameter

  4. Type the parameter name in the box with a red border.
  5. 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.

Extracting a parameter in Ruby using the Extract Parameter dialog

To be able to use the Extract Parameter dialog (instead of performing the refactoring in-place), make sure that the Enable in place refactorings option is off in the editor settings.

Once this is the case, you perform the Extract Parameter refactoring as follows:

  1. In the editor, place the cursor within the expression to be replaced by a parameter.
  2. Do one of the following:
    • Press Ctrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PCtrl+Alt+PAlt+Meta+PAlt Command PAlt Command PAlt Command PAlt Command P.
    • Choose Refactor | Extract | Parameter in the main menu.
    • Choose Refactor | Extract | Parameter from the context menu.
  3. 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.
  4. In the Extract Parameter dialog:
    1. Specify the parameter name in the Name field.
    2. If more than one occurrence of the expression is found within the function body, you can choose to replace only the selected occurrence or all the found occurrences with the references to the new parameter. Use the Replace all occurrences check box to specify your intention.
  5. Preview and apply changes.

See Also

Code Examples:

Procedures:

Reference:

Web Resources: