> This page is for SDK, version Current (default).
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# 添加自定义代码

> 使用自定义工具增强您的 Ruby SDK

本页面介绍如何向您的 Ruby SDK 添加自定义逻辑、方法和依赖项。

> **Note**
>
> These steps cover adding a new custom file to the SDK. To preserve line-level edits to a generated file, use [Replay](/learn/sdks/overview/custom-code#replay) instead.

## 添加自定义逻辑

开始添加自定义代码：

#### 创建新文件并添加自定义逻辑

**`lib/<gem_name>/helper.rb`**

```ruby title="lib/<gem_name>/helper.rb"
module YourGem
  class Helper
    def self.my_helper
      puts "Hello World!"
    end
  end
end
```

#### 将文件添加到 \`.fernignore\`

**`.fernignore`**

```yaml {3} title=".fernignore"
# 指定不应被 Fern 修改的文件

lib/<gem_name>/helper.rb
```

#### 使用辅助方法

Now your users can consume the helper function by importing it from the SDK.

```ruby
require "your_gem"

YourGem::Helper.my_helper
```

## 添加自定义 SDK 方法

Fern also allows you to add custom methods to the SDK itself (e.g.
`client.my_method()` ) by inheriting the Fern generated client and then
extending it.

#### 更新 \`generators.yml\` 配置

将您的 Fern 生成的客户端命名为 `BaseClient` 之类的名称，以反映该客户端将被扩展。

**`generators.yml`**

```yaml {4} title="generators.yml"
- name: fern-ruby-sdk
  version: 1.40.1
  config:
    clientClassName: BaseClient
```

#### 导入并扩展生成的客户端

首先，导入 Fern 生成的基础客户端并扩展它。然后，添加您想要的任何方法。

**`lib/<gem_name>/my_client.rb`**

```ruby title="lib/<gem_name>/my_client.rb"
require_relative "client"

module YourGem
  class MyClient < BaseClient
    def my_helper
      puts "Hello World!"
    end
  end
end
```

#### 更新 \`.fernignore\`

将 `my_client.rb` 添加到 `.fernignore`。

**`.fernignore`**

```diff title=".fernignore"
+ lib/<gem_name>/my_client.rb
```

#### 使用方法

您的用户需要构造扩展的客户端，而不是生成的客户端。

```ruby
require "your_gem"

client = YourGem::MyClient.new
```

Now your users can consume the helper function by importing it from the SDK.

```ruby
client.my_helper
```

## 添加自定义依赖项

> **企业功能**
>
> 此功能仅适用于[企业计划](https://buildwithfern.com/pricing)。如需开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。

要添加自定义代码所需的 gem，请更新您的 `generators.yml`。

**`generators.yml`**

```yaml {4-7} title="generators.yml"
- name: fern-ruby-sdk
  version: 1.40.1
  config:
    extraDependencies:
      faraday: ">= 1.0"
    extraDevDependencies:
      rspec: "~> 3.0"
```