# 提取字段

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

> **TL;DR**
> `重构 | 提取/引入 | 字段`
>
>
>
> `Ctrl+Alt+F` (Windows), `⌘ ⌥ F` (macOS), `⌘ ⌥ F` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ F` (macOS System Shortcuts), `Ctrl+Alt+F` (XWin), `Ctrl+Alt+F` (GNOME), `Ctrl+Alt+F` (KDE), `Ctrl+Alt+F` (Emacs), `Ctrl+Alt+F` (Sublime Text), `Ctrl+Alt+F` (Sublime Text (macOS)), `Ctrl+R, F` (Visual Studio), `⌘ R, F` (Visual Studio (macOS)), `Ctrl+Alt+F` (TextMate), `Alt+Shift+E` (NetBeans), `Ctrl+Alt+F` (Eclipse), `⌘ ⌥ F` (Eclipse (macOS))

提取字段 重构允许您将指定的值提取为实例或类变量。

下面的动画演示了如何将字符串中的特定单词提取为实例变量：

![提取字段](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_field.animated.gif)

要提取字段：

Procedure:

1. 选择要重构的值，或将插入符号放置在包含所需值的字符串中。

2. 在主菜单中，前往  `重构 | 提取/引入 | 字段`  `Ctrl+Alt+F` (Windows), `⌘ ⌥ F` (macOS), `⌘ ⌥ F` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ F` (macOS System Shortcuts), `Ctrl+Alt+F` (XWin), `Ctrl+Alt+F` (GNOME), `Ctrl+Alt+F` (KDE), `Ctrl+Alt+F` (Emacs), `Ctrl+Alt+F` (Sublime Text), `Ctrl+Alt+F` (Sublime Text (macOS)), `Ctrl+R, F` (Visual Studio), `⌘ R, F` (Visual Studio (macOS)), `Ctrl+Alt+F` (TextMate), `Alt+Shift+E` (NetBeans), `Ctrl+Alt+F` (Eclipse), `⌘ ⌥ F` (Eclipse (macOS))。

3. （可选） 如果您将插入符号放置在包含多个可提取表达式的位置，请选择所需的表达式并按下 `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))。

4. （可选）  如果找到多个表达式的出现位置，请指定您希望仅替换选定的出现位置，还是用新变量替换所有找到的出现位置：

![替换所有出现的地方](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_field.png)

5. 指定实例/类变量名称并按下 `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))。

## 示例

Before:

```RUBY
class Hello
    def greet
        puts "Hello from JetBrains"
    end
    def bye
        puts "Goodbye from JetBrains"
    end
end

```

After:

```RUBY
class Hello
    def initialize
        @name = "JetBrains"
    end

    def greet
        puts "Hello from #{@name}"
    end
    def bye
        puts "Goodbye from #{@name}"
    end
end

```

## See also

### 参考

