# Extract RSpec 'let'

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

> **TL;DR**
> `Refactor | Extract/Introduce | 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))

The Extract RSpec 'let' refactoring allows you to extract a specified code fragment into a memoized helper method. To do this, perform the following steps:

Procedure:

1. In a spec file, select a required code fragment and select `Refactor | Refactor This | 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))) from the main menu.

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

2. Specify the desired name of a helper method 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)).

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

3. If more than one occurrence of the code fragment is found, RubyMine will suggest replacing these occurrences with a helper method call.

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

Click Yes to replace the found occurrences. If you want to replace the selected occurrence only, click No.

> **Note:**
> If you click Yes, RubyMine suggests replacing the found occurrences one by one or all at a time.

## Example

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
```

