• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

Ruby SDK reference

Read time: 2 minutes
Last edited: Jan 25, 2022

This reference guide documents all of the methods available in the Ruby SDK, and explains how these methods work. LaunchDarkly's SDKs are open source. The source is available in its GitHub repository and its generated API reference.

You can also use this SDK to clone and run a sample application.

RubyGems bug

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

Getting started

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

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'

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("YOUR_SDK_KEY")
LDClient must be a singleton

It's important to make LDClient a singleton. 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.

You can use client to check which variation a particular user will receive for a given feature flag.

Here's how:

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

Lastly, when your application is about to terminate, shut down 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.

Here's how:

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

Initializing LDClient in a Rails Application

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

Rails.configuration.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:

Spring.after_fork do
Rails.configuration.client = LaunchDarkly::LDClient.new('SDK KEY')
end

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

after_fork do |server,worker|
Rails.configuration.client = LaunchDarkly::LDClient.new('SDK KEY')
end

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')
end

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')
end
end

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.

Lastly, shut down the client when your application terminates. To learn more, read Shutting down.

Supported features

This SDK supports the following features: