# RSpec の 'let' を抽出する

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

> **TL;DR**
> `リファクタリング | 抽出 / 導入 | RSpec 'let'`
>
>
>
> `⌘ ⇧ L` (Windows), `⌘ ⇧ L` (macOS), `⌘ ⇧ L` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ ⌥ L` (macOS System Shortcuts), `⌘ ⇧ L` (XWin), `⌘ ⇧ L` (GNOME), `⌘ ⇧ L` (KDE), `⌘ ⇧ L` (Emacs), `⌘ ⇧ L` (Sublime Text), `⌘ ⇧ L` (Sublime Text (macOS)), `⌘ ⇧ L` (Visual Studio), `⌘ ⇧ L` (Visual Studio (macOS)), `⌘ ⇧ L` (TextMate), `⌘ ⇧ L` (NetBeans), `⌘ ⇧ L` (Eclipse), `⌘ ⇧ L` (Eclipse (macOS))

RSpec 'let' を抽出する リファクタリングにより、指定されたコードフラグメントをメモ化されたヘルパーメソッドに抽出できます。 これを行うには、次の手順を実行します。

Procedure:

1. スペックファイルで、必要なコード箇所を選択し、メインメニューから  `リファクタリング | これをリファクタリング | RSpec 'let'`  ( `⌘ ⇧ L` (Windows), `⌘ ⇧ L` (macOS), `⌘ ⇧ L` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ ⌥ L` (macOS System Shortcuts), `⌘ ⇧ L` (XWin), `⌘ ⇧ L` (GNOME), `⌘ ⇧ L` (KDE), `⌘ ⇧ L` (Emacs), `⌘ ⇧ L` (Sublime Text), `⌘ ⇧ L` (Sublime Text (macOS)), `⌘ ⇧ L` (Visual Studio), `⌘ ⇧ L` (Visual Studio (macOS)), `⌘ ⇧ L` (TextMate), `⌘ ⇧ L` (NetBeans), `⌘ ⇧ L` (Eclipse), `⌘ ⇧ L` (Eclipse (macOS))) を選択します。

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

2. ヘルパーメソッドの目的の名前を指定し、 `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)) を押します。

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

3. コード箇所が複数見つかった場合、RubyMine はそれらをコード編集ヘルパーメソッド呼び出しに置き換えることを提案します。

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

見つかった出現箇所を置き換えるには、   はい   をクリックします。 選択した出現箇所のみを置換する場合は、   ノー   をクリックします。

> **Note:**
> はい   をクリックすると、RubyMine は見つかった出現箇所を順に、またはすべて一度に置き換えることを提案します。

## サンプル

Before:

```RUBY
describe "GetTime" do
    it "gets the same time" do
        puts Time.now
        sleep(3)
        puts Time.now
    end
end

```

After:

```RUBY
describe "GetTime" do
    let(:current_time) { Time.now }

    it "gets the same time" do
        puts current_time
        sleep(3)
        puts current_time
    end
end

```

