# メソッドの抽出​

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

> **TL;DR**
> `リファクタリング | 抽出 / 導入 | メソッド`
>
>
>
> `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))

メソッドの抽出 リファクタリングを使用すると、指定したコードフラグメントを独自のメソッドに抽出できます。

メソッドを抽出するには

Procedure:

1. リファクタリングするコードフラグメントを選択するか、必要なコードフラグメントを含む文字列にキャレットを置きます。

![メソッド抽出のための選択コード](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_method_select_code.png)

2. メインメニューで、  `リファクタリング | 抽出 / 導入 | メソッド`  `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. メソッドの抽出   ダイアログで、メソッドの可視性、名前、オプションでパラメーター名を指定します。

![メソッドの抽出ダイアログ](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_method_dialog.png)

4. OK   をクリックしてリファクタリングを終了します。

![メソッドの抽出​](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_method_result.png)

## サンプル

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

```

