• SDKS
No results for ""


Ruby SDK reference

Read time: 8 minutes
Last edited: May 14, 2021

This reference guide documents all of the methods available in our Ruby SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source. To learn more, read Ruby SDK GitHub repository. The online Ruby API docs contain the programmatic definitions of every class and method. Additionally you can clone and run a sample application using this SDK.


Due to a bug in recent versions of RubyGems, versions 2.4.x (of RubyGems, not of Ruby) are not compatible with the LaunchDarkly Ruby SDK.

Getting started

Building on top of our Getting Started guide, the following steps will get you started with using the LaunchDarkly SDK in your Ruby application.

The first step is to install the LaunchDarkly SDK as a dependency in your application using your application's dependency manager. Refer to the SDK releases page to identify the latest version if you want to depend on a specific version.

If you are using Bundler, you can add gem "launchdarkly-server-sdk" to your Gemfile and run bundle install. Otherwise, you can install the gem directly:

1gem install launchdarkly-server-sdk

Next you should import the LaunchDarkly client in your application code. (This step may not be necessary if you are using a framework that automatically loads all dependencies, as Rails does.)

1require 'ldclient-rb'

Once the SDK is installed and imported, you'll want to create a single, shared instance of LDClient. You should specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

1ld_client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY")
LDClient must be a singleton

It's important to make this a singleton. The client instance maintains internal state that allows us to serve feature flags without making any remote requests. Be sure that you're not instantiating a new client with every request.

Using ld_client, you can check which variation a particular user should receive for a given feature flag.

1show_feature = ld_client.variation("your.flag.key", {key: "user@test.com"}, false)
2if show_feature
3 # application code to show the feature
5 # the code to run if the feature is off

Lastly, when your application is about to terminate, shut down ld_client. This ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events. This is something you only need to do once.

1# shut down the client, since we're about to quit

Initializing LDClient in a Rails Application

To use LaunchDarkly in a Rails application, initialize the client in config/initializers/launchdarkly.rb:

1Rails.configuration.ld_client = LaunchDarkly::LDClient.new("your_sdk_key")

To use LaunchDarkly with the Rails application preloader Spring, we recommend using an after_fork callback in the config/spring.rb file:

1Spring.after_fork do
2 Rails.configuration.ld_client = LaunchDarkly::LDClient.new('SDK KEY')

Similarly, with Unicorn, you'll need to specify an after_fork hook in your unicorn.rb config file:

1after_fork do |server,worker|
2 Rails.configuration.ld_client = LaunchDarkly::LDClient.new('SDK KEY')

If you're using the Puma web server, we recommend initializing the client in on_worker_boot, as well as initializing in the Rails app:

1on_worker_boot do
2 Rails.configuration.ld_client = LaunchDarkly::LDClient.new('SDK KEY')

If you're using the Passenger web server, we recommend initializing the client in config.ru, or from any code called while loading config.ru:

1if defined?(PhusionPassenger)
2 PhusionPassenger.on_event(:starting_worker_process) do |forked|
3 Rails.configuration.ld_client = LaunchDarkly::LDClient.new('SDK KEY')
4 end

Customizing your client

You can also customize the behavior of the client by creating a custom configuration object:

1config = LaunchDarkly::Config.new({connect_timeout: 1, read_timeout: 1})
2ld_client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY", config)

The client constructor takes a configuration object as an optional parameter. In this example, we've set the connection timeout to LaunchDarkly to 1 second, and the read timeout to 2 seconds.

To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.


Let's walk through this snippet. The most important attribute is the user key. In this case, we've used the hash "aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an email address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like firstName, email, and the custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

Besides the key, LaunchDarkly supports the following attributes at the "top level". Remember, all of these are optional:

  • ip: Must be an IP address.
  • firstName: Must be a string. If you provide a first name, you can search for users on the Users page by name.
  • lastName: Must be a string. If you provide a last name, you can search for users on the Users page by name.
  • country: Must be a string representing the country associated with the user.
  • email: Must be a string representing the user's email address. If an avatar URL is not provided, we'll use Gravatar to try to display an avatar for the user on the Users page.
  • avatar: Must be an absolute URL to an avatar image for the user.
  • name: Must be a string. You can search for users on the User page by name
  • anonymous: Must be a boolean. See the section below on anonymous users for more details.

In addition to built-in attributes, you can pass us any of your own user data by passing custom attributes, like the groups attribute in the example above.

A note on attribute keys

All user attribute keys (both built-in and custom attributes) must be symbols and not strings.

A note on types

Most of our built-in attributes (like names and email addresses) expect string values. Custom attribute values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers. If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us, including organizations, groups, andaccount plans. Anything you pass to us becomes available instantly on our dashboard.

Private User Attributes

You can optionally configure the Ruby SDK to treat some or all user attributes as Private user attributes. Private user attributes can be used for targeting purposes, but are removed from the user data sent back to LaunchDarkly.

In the Ruby SDK there are two ways to define private attributes for the entire LaunchDarkly client:

  • In the LaunchDarkly config, you can set all_attributes_private to true. If this is enabled, all user attributes (except the key) for all users are removed before the user is sent to LaunchDarkly.
  • In the LaunchDarkly config object, you can define a list of private_attribute_names. If any user has a custom or built-in attribute named in this list, it will be removed before the user is sent to LaunchDarkly.

You can also define a set of privateAttributeNames on the user object itself. For example:

1user = {
2 key: "aa0ceb",
3 firstName: "Ernestina",
4 lastName: "Evans",
5 email: "ernestina@example.com",
6 custom: {
7 groups: ["Google", "Microsoft"]
8 },
9 privateAttributeNames: ['email']

When this user is sent back to LaunchDarkly, the email attribute will be removed.

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

1user = { key: "aa0ceb", anonymous: true }

You will still need to generate a unique key for anonymous users. Session IDs or UUIDs work best for this.

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. You also can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. This is actually a good thing, because it keeps anonymous users from polluting your Users page!

Aliased users

There are situations in which multiple LaunchDarkly users can represent one person. For example, this can happen when a person initially logs into an application. The person might be represented by an anonymous user before they log in, and a different user after they log in. In that case, that one person would be identified by two different users as denoted by different user keys.

The SDK can associate these two LaunchDarkly users by sending an alias event. You can manually tell the SDK to send an alias event with the alias method.

1ld_client.alias(new_user, previous_user)


The variation method determines which variation of a feature flag a user receives.

1value = ld_client.variation("your.feature.key", user, false)

variation calls take the feature flag key, a user, and a default value.

The default value will only be returned if an error is encountered. For example, the default value returns if the feature flag key doesn't exist or the user doesn't have a key specified.

The variation call will automatically create a user in LaunchDarkly if a user with that user key doesn't exist already. There's no need to create users ahead of time (but if you do need to, take a look at Identify).


The variation_detail method allows you to evaluate a feature flag (using the same parameters as you would for variation) and receive more information about how the value was calculated.

The variation detail is returned in an object that contains both the result value and a "reason" object which will tell you, for instance, if the user was individually targeted for the flag or was matched by one of the flag's rules. It will also indicate if the flag returned the default value due to an error. You can examine the "reason" data programmatically; you can also view it with Data Export, if you are capturing detailed analytics events for this flag.

To learn more, read Evaluation reasons.


The track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. Here's a simple example:

1ld_client.track("your-goal-key", user)

You can also attach an extra hash containing arbitrary data to your event:

1ld_client.track("Completed purchase", user, {price: 320})


The identify method creates or updates users on LaunchDarkly, making them available for targeting and autocomplete on the dashboard. In most cases, you won't need to call identify. The variation call will automatically create users on the dashboard for you. identify can be useful if you want to pre-populate your dashboard before launching any features.


All flags

Creating users

Unlike variation and identify calls, all_flags_state does not send events to LaunchDarkly. Thus, users are not created or updated in the LaunchDarkly dashboard.

The all_flags_state method captures the state of all feature flag keys with regard to a specific user. This includes their values, as well as other metadata.

This method can be useful for passing feature flags to your front-end. In particular, it can be used to provide bootstrap flag settings for our JavaScript SDK.

1state = ld_client.all_flags_state(user)

Secure mode hash

The secure_mode_hash method computes an HMAC signature of a user signed with the client's SDK key. If you're using our JavaScript SDK for client-side flags, this method generates the signature you need for secure mode.



Internally, the LaunchDarkly SDK keeps an event buffer for track and identify calls. These are flushed periodically in a background thread. In some situations (for example, if you're testing out the SDK in a REPL), you may want to manually call flush to process events immediately.


The flush interval is configurable. If you need to change the interval, you can do so when configuring your client instance.

Offline mode

In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. Offline mode lets you do this easily.

1config = LaunchDarkly::Config.new({offline: true})
2ld_client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY", config)
3ld_client.variation("any.feature.flag", user, false) # will always return the default value (false)

Proxy configuration

The Ruby SDK uses the standard Ruby mechanism for specifying an HTTP/HTTPS proxy: set the environment variable HTTPS_PROXY or HTTP_PROXY to the URL of the proxy server. All LaunchDarkly services use HTTPS, so HTTPS_PROXY is the variable to set unless you are configuring the SDK to use a custom service URL that is not secure, such as a Relay Proxy instance within your private network.


Close safely shuts down the client instance and releases all resources associated with the client. In most long-running applications, you should not have to call close.



The Ruby SDK uses Ruby's built-in Logger class. All loggers are namespaced under [LDClient]. A custom logger may be passed to the SDK by the configurable logger property:

1log = ::Logger.new($stdout)
2log.level = ::Logger::DEBUG
3config = LaunchDarkly::Config.new({logger: log})
4client = LaunchDarkly::LDClient.new("YOUR_SDK_KEY", config)

Be aware of two considerations when enabling the DEBUG log level:

  1. Debug-level logs can be very verbose. It is not recommended that you turn on debug logging in high-volume environments.
  2. Potentially sensitive information is logged including LaunchDarkly users created by you in your usage of this SDK.

Database integrations

Feature flag data can be kept in a persistent store using Redis, DynamoDB, or Consul. These adapters are implemented in the LaunchDarkly::Integrations::Redis, LaunchDarkly::Integrations::DynamoDB, and LaunchDarkly::Integrations::Consul modules; to use them, call the new_feature_store method in the module, and put the returned object in the feature_store property of your client configuration.

To learn more, read the API documentation and "Using a persistent feature store".