Ruby SDK reference
Read time: 3 minutes
Last edited: Jan 20, 2023
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product.
You can upgrade your SDK at any time. However, the ability to target by context, or review context instances that have encountered flags in your application, is only available for customers in the contexts Early Access Program (EAP). If you want access this feature, join the EAP. If you are not part of the EAP, your application can send user contexts to LaunchDarkly and they will appear on the Users list.
Code samples on this page are from the two most recent SDK versions where they differ. To learn more about upgrading, read Ruby SDK 6.x to 7.0 migration guide.
This topic documents how to get started with the server-side Ruby SDK, and links to reference information on all of the supported features.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
Due to a bug in recent versions of RubyGems, RubyGems versions 2.4.x are not compatible with the LaunchDarkly Ruby SDK.
The LaunchDarkly Ruby SDK, version 7.0 and higher, 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.
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.
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.
client = LaunchDarkly::LDClient.new("sdk-key-123abc")
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.
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 User and context configuration.
context = LaunchDarkly::LDContext.with_key("email@example.com")show_feature = client.variation("your.flag.key", context, false)if show_feature# application code to show the featureelse# the code to run if the feature is off
To use LaunchDarkly in a Rails application, initialize the client in
Rails.configuration.client = LaunchDarkly::LDClient.new("sdk-key-123abc")
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 doRails.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 doRails.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
if defined?(PhusionPassenger)PhusionPassenger.on_event(:starting_worker_process) do |forked|Rails.configuration.client = LaunchDarkly::LDClient.new('SDK KEY')endend
Lastly, 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.
This SDK supports the following features:
- Big Segments
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging configuration
- Offline mode
- Reading flags from a file
- Relay Proxy configuration
- Secure mode
- Sending custom events
- Service endpoint configuration
- Shutting down
- Storing data
- Test data sources
- User and context configuration
- Web proxy configuration