---
title: Real-Time Audiences for Feature Experimentation segment qualification methods for the Ruby SDK
slug: feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-ruby
docTags: 
createdAt: 2026-08-05T20:40:00.000Z
---

## Prerequisites

You must enable and configure [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8) before fetching qualified audience segments and checking if the user is qualified for the given audience segment.

## fetch\_qualified\_segments

### Minimum SDK version

5.0.0

### Description

You can use the `fetch_qualified_segments` method to retrieve the external audience mapping for a specific user from the ODP server. The Optimizely Feature Experimentation Ruby SDK lets you make the call to ODP in a synchronous or asynchronous fashion, depending on the block parameter.

- If a block is not provided, the caller is blocked until the synchronous fetch is completed.
- If a block is provided, the caller is not blocked.

`fetch_qualified_segments` is a method of the `OptimizelyUserContext` object. See [OptimizelyUserContext](docId\:CBB2m_VFyZWW2HqXI529Q) for details.

### Parameters

The following table describes the parameters for the `fetch_qualified_segments` method:

| Parameter              | Type              | Description                                                         |
| ---------------------- | ----------------- | ------------------------------------------------------------------- |
| \`options\` (optional) | String            | A set of options for fetching qualified audience segments from ODP. |
| \`block \` (optional)  | Callback function | A completion handler to be called with the fetch result.            |

### Returns  –  Synchronous call

If the Ruby SDK does not have a block, the`fetch_qualified_segments` method returns `true` if the qualified segments array in the [user context](docId\:CBB2m_VFyZWW2HqXI529Q) was updated.

### Returns  –   Asynchronous call

If the Ruby SDK has a block, the `fetch_qualified_segments` method fetches the audience segments on a new thread and returns the thread handle.

- If the fetch completes, the Ruby SDK updates the qualified segments array in the [user context](docId\:CBB2m_VFyZWW2HqXI529Q) and then calls the block with a success status.
- If the fetch fails, the SDK calls the block with a failure status.

If the Ruby SDK does not find an ODP audience in the datafile, it returns an empty qualified segments array without sending a request to the ODP server.

:::hint{type="info"}
You can read and write directly to the qualified segments array instead of calling `fetch_qualified_segments`.

This lets you bypass the remote fetching process from ODP or use your own fetching service. This can be helpful when testing or debugging.
:::

### Example `fetch_qualified_segments` call

The code creates a user context by instantiating an OptimizelyUserContext object with a user ID "user123" and a set of attributes, which includes an "app\_version" attribute with a value of "1.3.2".

The next section of the code shows the use of the`fetch_qualified_segments` method to fetch all qualified segments with and without segment options.

After that, the code uses the [\`decide\`](docId\:W-MCwQUNp-EC5yaDomerg) method to decide whether to show the feature flag with the key "flag1" to the user. Finally, the SDK calls the [\`track\_event\`](docId\:e0Bu1wqkxwIdIw4inzSIC) method to track a custom event called "myevent".

If a callback is provided, the fetch runs on a spawned thread and runs the callback upon completion. If no callback is provided, the fetch is synchronous.

```ruby
attributes = { 'app_version' => '1.3.2' }
user = optimizely.create_user_context('fs-id-123', attributes)

# spawned thread is returned
fetch_thread = user.fetch_qualified_segments do |success|
  puts success

  return unless success

  decision = user.decide('flag1')
  user.track_event('purchase_event')
end


# thread must eventually be joined to ensure callback is run to completion
fetch_thread.join
```

```ruby
attributes = { "app_version"=> "1.3.2" }
user = optimizely.create_user_context("fs-id-123", attributes)

# Without segment option
success = user.fetch_qualified_segments

# With segment options
odp_segment_options = [Optimizely::OptimizelyOdpOption.IGNORE_CACHE, Optimizely::OptimizelyOdpOption.RESET_CACHE]

success = user.fetch_qualified_segments(options: odp_segment_options)

if success
    decision = user.decide("flag1")
    user.track_event("myevent")
end
```

The following diagram shows the network calls between your application, the Feature Experimentation Ruby SDK, and the ODP server when calling `fetch_qualified_segments`:

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/iFltGnfEKXokfejb4ok1a_b33b9a9831a25b9fa6e7e139db06b39c8e1f3ce3c61fa257ab42012e8af0a160-image.png)

![](https://api.archbee.com/api/optimize/BrbvVORKJ5iIeSMZye5kI/e1V1oVShmSccScqHdQblO_ac49e56ac1af79188d771b33866276016e81d0d906c1980fbbde2afb68c5b800-image.png)

1. Your application calls the `fetch_qualified_segments` method.
2. The Feature Experimentation Ruby SDK makes a GraphQL call to ODP to fetch segments the user ID qualifies for.
3. ODP responds with the qualified audience segments.
4. The Ruby SDK automatically caches the fetched segments mapping the user ID to the qualified segments.
5. The `fetch_qualified_segments` method returns a boolean if the qualified segments array was updated.
6. Call the `decide` method to get the flag decision for a given user, a decision object that contains the flag's status (enabled or disabled), and the assigned variation key.

The Ruby SDK fetches the audience segments and then caches them. This means that if you request the segments again (when new user contexts are created), you can retrieve the audience segment information from the cache rather than from the remote ODP server.

The cache is used for the `fetch_qualified_segments` call. This method is called on the user context (the user context is fixed, including the real-time audiences the user qualifies for).

The cache *only* applies when calling the `fetch_qualified_segments` call. If you set the cache timeout to 0, the cache is disabled. Optimizely uses the least recently used (LRU) algorithm, so the oldest record is bumped out when the maximum size is reached. If there is a cache miss upon the method call, Optimizely makes a network request.

You can add the following options to your `OptimizelyOdpOption` array to bypass caching:

- **IGNORE\_CACHE** – Bypass segments cache for lookup and save.
- **RESET\_CACHE** – Reset all segments cache.

## qualified\_for?

### Minimum SDK version

5.0.0

### Description

Check if the user is qualified for the given audience segment.

### Parameters

The following table describes the parameters for the `qualified_for?` method:

| Parameter | Type   | Description                                                          |
| --------- | ------ | -------------------------------------------------------------------- |
| `segment` | String | The ODP audience segment name to check if the user is qualified for. |

### Returns

`true` if the user is qualified.

### Examples

The following is an example of whether the user is qualified for an ODP segment:

```ruby
attributes = {'laptop_os' => 'mac'}
user = optimizely.create_user_context('fs-id-12', attributes)

success = user.fetch_qualified_segments
qualified = user.qualified_for?('segment1');
```

## Source files

The language and platform source files containing the implementation for Ruby are available on [GitHub](https://github.com/optimizely/ruby-sdk/blob/master/lib/optimizely/odp/odp_segment_manager.rb).
