RubyMine 2024.2 Help

Extract field

The Extract Field refactoring lets you extract a specified value to an instance or class variable.

The animation below demonstrates how to extract a specific word from a string to an instance variable:

extract field

To extract a field:

  1. Select a value to refactor or place the caret at a string containing the required value.

  2. In the main menu, go to Refactor | Extract/Introduce | Field Ctrl+Alt+F.

  3. (Optional) If you place the caret at a place containing several expressions available for extracting, select the required expression and press Enter.

  4. (Optional) If more than one occurrence of the expression is found, specify whether you wish to replace only the selected occurrence or all the found occurrences with the new variable:

    replace all occurrences
  5. Specify the instance/class variable name and press Enter.

Example

class Hello def greet puts "Hello from JetBrains" end def bye puts "Goodbye from JetBrains" end end
class Hello def initialize @name = "JetBrains" end def greet puts "Hello from #{@name}" end def bye puts "Goodbye from #{@name}" end end
Last modified: 08 October 2024