• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL

EDIT ON GITHUB

Ruby SDK reference

Read time: 2 minutes
Last edited: Jul 28, 2021

This reference guide documents all of the methods available in the 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.

RubyGems

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.

Here's how:

1require 'ldclient-rb'

Once the SDK is installed and imported, 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.

Here's how:

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
4else
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.

Here's how:

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

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

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

If you use 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')
3end

If you use 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
5end

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: