# Extract constant

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/help/ruby/2026.3/llms.txt).

> **TL;DR**
> `Refactor | Extract/Introduce | Constant`
>
>
>
> `Ctrl+Alt+C` (Windows), `⌘ ⌥ C` (macOS), `⌘ ⌥ C` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ C` (macOS System Shortcuts), `Ctrl+Alt+C` (XWin), `Ctrl+Alt+C` (GNOME), `Ctrl+Alt+C` (KDE), `Ctrl+Alt+C` (Emacs), `Ctrl+Alt+C` (Sublime Text), `⌘ ⌥ C` (Sublime Text (macOS)), `Ctrl+Alt+C` (Visual Studio), `⌘ ⌥ C` (Visual Studio (macOS)), `Ctrl+Alt+C` (TextMate), `Alt+Shift+C` (NetBeans), `Ctrl+Alt+C` (Eclipse), `Ctrl+Alt+C` (Eclipse (macOS))

As [Extract Variable](extract-variable.html), 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](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_constant.animated.gif)

To extract a constant:

Procedure:

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` `Ctrl+Alt+C` (Windows), `⌘ ⌥ C` (macOS), `⌘ ⌥ C` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ C` (macOS System Shortcuts), `Ctrl+Alt+C` (XWin), `Ctrl+Alt+C` (GNOME), `Ctrl+Alt+C` (KDE), `Ctrl+Alt+C` (Emacs), `Ctrl+Alt+C` (Sublime Text), `⌘ ⌥ C` (Sublime Text (macOS)), `Ctrl+Alt+C` (Visual Studio), `⌘ ⌥ C` (Visual Studio (macOS)), `Ctrl+Alt+C` (TextMate), `Alt+Shift+C` (NetBeans), `Ctrl+Alt+C` (Eclipse), `Ctrl+Alt+C` (Eclipse (macOS)).

3. (Optional) If you place the caret at a place containing several expressions available for extracting, select the required expression and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)):

![expressions for extracting](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_constant_expressions.png)

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](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_constant.png)

5. Specify the constant name and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

## Example

Before:

```RUBY
puts "Hello from JetBrains"
puts "Goodbye from JetBrains"
```

After:

```RUBY
NAME = "JetBrains"
puts "Hello from #{NAME}"
puts "Goodbye from #{NAME}"
```

## See also

### External Links

