No results for ""
EXPAND ALL
  • Home
  • API docs

Ruby SDK reference

Read time: 6 minutes
Last edited: Sep 04, 2024
Recent major versions

Version 8 of the Ruby SDK supports migration feature flags. These are temporary flags used to migrate data or systems while keeping your application available and disruption free. To learn more about upgrading, read Ruby SDK 7.x to 8.0 migration guide.

Version 7 of the Ruby SDK replaces users with contexts. A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more about upgrading, read Ruby SDK 6.x to 7.0 migration guide and Best practices for upgrading users to contexts.

Code samples on this page are from the three most recent SDK versions where they differ.

Overview

This topic documents how to get started with the server-side Ruby SDK, and links to reference information on all of the supported features.

SDK quick links

LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositoryruby-server-sdk
Sample applicationsRuby
Rails with bootstrapping
Published moduleRubyGems
SDK version compatibility

The LaunchDarkly Ruby SDK, version 8.0 and higher, is compatible with Ruby 3.0.

The LaunchDarkly Ruby SDK, version 7.x, is compatible with Ruby 2.7 and higher.

Prior to version 7.0, the LaunchDarkly Ruby SDK also supported Ruby 2.5 and 2.6.

Get started

After you complete the Get started process, follow these instructions to start using the LaunchDarkly SDK in your Ruby application.

Install the SDK

First, 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:

gem install launchdarkly-server-sdk

Next, 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.

Here's how:

require 'ldclient-rb'
The Ruby SDK uses an SDK key

The Ruby SDK uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

Initialize the client

After you install and import the SDK, create a single, shared instance of LDClient. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.

Here's how:

client = LaunchDarkly::LDClient.new("sdk-key-123abc")

To learn more about the specific configuration options available in this SDK, read Config.

LDClient must be a singleton

It's important to make LDClient a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

If you have multiple LaunchDarkly projects, you can create one LDClient for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.

Initialize the client while using a Rails application

If you are using a Rails application, do not use the above method to initialize the client. Instead, follow the instructions below for your application.

Expand Using a Rails application

Using a Rails application



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

Rails.configuration.client = LaunchDarkly::LDClient.new("sdk-key-123abc")
Expand Using Spring

Using Spring



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

Spring.after_fork do
Rails.configuration.client = LaunchDarkly::LDClient.new('sdk-key-123abc')
end
Expand Using Unicorn

Using Unicorn



If you use Unicorn, specify an after_fork hook in your unicorn.rb config file:

after_fork do |server,worker|
Rails.configuration.client = LaunchDarkly::LDClient.new('sdk-key-123abc')
end
Expand Using Puma

Using Puma



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

on_worker_boot do
Rails.configuration.client = LaunchDarkly::LDClient.new('sdk-key-123abc')
end
Expand Using Passenger

Using Passenger



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

if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
Rails.configuration.client = LaunchDarkly::LDClient.new('sdk-key-123abc')
end
end

Initialize the client while using httplog

If you are using the Rails httplog library, you should include launchdarkly.com in the url_blacklist_pattern attribute of httplog's configuration.

By default, the Rails httplog library buffers the entire response to a request. However, in the LaunchDarkly SDKs, the streaming request remains open. Therefore, the httplog library intercepts the request but never returns the response from LaunchDarkly indicating that initialization is complete. This means the Ruby SDK may not complete initialization and also may not log an error. If your application makes flag evaluations before the SDK initialization is complete, you may receive the message: [LDClient] Client has not finished initializing; feature store unavailable, returning default value.

When you include launchdarkly.com in the url_blacklist_pattern attribute of httplog's configuration, then httplog will not intercept the response, and SDK initialization will complete. This lets you use flags as expected.

To learn more, read the httplog Configuration documentation.

Evaluate a context

You can use client to check which variation a particular context will receive for a given feature flag. To learn more, read Evaluating flags and Flag evaluation reasons. For more information about how contexts are specified, read Context configuration.

Here's how:

context = LaunchDarkly::LDContext.with_key("user-key-123abc")
show_feature = client.variation("flag-key-123abc", context, false)
if show_feature
# application code to show the feature
else
# the code to run if the feature is off

Shut down the client

Shut down the client when your application terminates. This frees the resources the worker threads were using and provides an explicit signal for the Ruby SDK to send the remaining event data back to LaunchDarkly. To learn more, read Shutting down.

Supported features

This SDK supports the following features: