RubyMine 2023.3 Help

Extract constant

As Extract Variable, the Extract Constant refactoring lets you extract a specified value to a corresponding constant.

The animation below demonstrates how to extract a specific word from a string to a constant:

extract constant

To extract a constant:

  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 | Constant Control+Alt+C.

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

    expressions for extracting
  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 constant name and press Enter.

Example

puts "Hello from JetBrains" puts "Goodbye from JetBrains"
NAME = "JetBrains" puts "Hello from #{NAME}" puts "Goodbye from #{NAME}"
Last modified: 11 January 2024