# Extract method

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

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

The Extract Method refactoring allows you to extract a specified code fragment into its own method.

To extract a method:

Procedure:

1. Select a code fragment to refactor or place the caret at a string containing the required code fragment:

![select code for a method extracting](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_method_select_code.png)

2. In the main menu, go to `Refactor | Extract/Introduce | Method` `Ctrl+Alt+M` (Windows), `⌘ ⌥ M` (macOS), `⌘ ⌥ M` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ M` (macOS System Shortcuts), `Ctrl+Alt+M` (XWin), `Ctrl+Alt+M` (GNOME), `Ctrl+Alt+M` (KDE), `Ctrl+Alt+M` (Emacs), `Ctrl+Alt+M` (Sublime Text), `⌘ ⌥ M` (Sublime Text (macOS)), `Ctrl+R, M` (Visual Studio), `⌘ R, M` (Visual Studio (macOS)), `Meta+Alt+M` (TextMate), `Alt+Shift+M` (NetBeans), `Alt+Shift+M` (Eclipse), `⌘ ⌥ M` (Eclipse (macOS)).

3. In the Extract Method dialog, specify the method visibility, name, and, optionally, parameter names:

![Extract Method dialog](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_method_dialog.png)

4. Click OK to finish refactoring:

![extract method](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_method_result.png)

## Example

Before:

```RUBY
class Hello
    def greet
        name = "JetBrains"
        puts "Hello from #{name}"
    end
end
```

After:

```RUBY
class Hello
    def greet
        name = "JetBrains"
        puts get_greeting(name)
    end

    private

    def get_greeting(name)
        "Hello from #{name}"
    end
end
```

