> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiIwNTQyYzA2Ni02YmJhLTQ5MTEtOGQ0NC0xZGZlMTY1ZTQzYjciLCJleHAiOjE3Nzg0MTQyOTMsImlhdCI6MTc3ODQxMzk5M30.ZZxBtuf2nRS8TnFfbGiJrFPeKtQ2RJKCgE197nn3q0k
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 添加自定义代码

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

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

<Info>
  Before getting started, 

  [read about how Fern SDKs use custom code and learn about the `.fernignore` file](/sdks/overview/custom-code)

  .
</Info>

## 添加自定义逻辑

开始添加自定义代码：

<Steps>
  <Step title="创建新文件并添加自定义逻辑">
    ```ruby title="lib/<gem_name>/helper.rb"
    module YourGem
      class Helper
        def self.my_helper
          puts "Hello World!"
        end
      end
    end
    ```
  </Step>

  <Step title="将文件添加到 `.fernignore`">
    ```yaml {3} title=".fernignore"
    # 指定不应被 Fern 修改的文件

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

  <Step title="使用辅助方法">
    Now your users can consume the helper function by importing it from the SDK.

    ```ruby
    require "your_gem"

    YourGem::Helper.my_helper
    ```
  </Step>
</Steps>

## 添加自定义 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.

<Steps>
  <Step title="更新 `generators.yml` 配置">
    将您的 Fern 生成的客户端命名为 `BaseClient` 之类的名称，以反映该客户端将被扩展。

    ```yaml {4} title="generators.yml"
    - name: fernapi/fern-ruby-sdk
      version: 1.11.1
      config:
        clientClassName: BaseClient
    ```
  </Step>

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

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

  <Step title="更新 `.fernignore`">
    将 `my_client.rb` 添加到 `.fernignore`。

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

  <Step title="使用方法">
    您的用户需要构造扩展的客户端，而不是生成的客户端。

    ```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
    ```
  </Step>
</Steps>

## 添加自定义依赖项

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

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

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