# Extract/Introduce variable

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

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

The Extract Variable refactoring lets you extract a specified value to a corresponding variable. You can use this refactoring for basic Ruby data types (for example, strings, arrays, hashes) and class instances.

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

![extract variable](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_variable.animated.gif)

To extract a variable:

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

5. Specify the variable 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

