Ruby
The Unleash Ruby SDK lets you evaluate feature flags in Ruby applications. It connects to Unleash or Unleash Edge to fetch flag configurations and evaluates them locally against an Unleash context.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
Requirements
- Ruby (MRI) 2.7 or later
- JRuby 9.4 or later
Installation
Add this line to your application’s Gemfile:
And then execute:
Or install it yourself as:
Configuration
It is required to configure:
app_namewith the name of the running applicationurlof your Unleash servercustom_http_headerswith{'Authorization': '<YOUR_API_TOKEN>'}when using Unleash v4+
It is highly recommended to configure:
instance_idparameter with a unique identifier for the running instance
or instantiate the client with the valid configuration:
Dynamic custom HTTP headers
If you need custom HTTP headers that change during the lifetime of the client, you can pass custom_http_headers as a Proc.
Configuration options
For a more in-depth look, please see lib/unleash/configuration.rb.
Usage in a plain Ruby application
Usage in a Rails application
Add initializer
The initializer setup varies depending on whether you’re using a standard setup, Puma in clustered mode, Phusion Passenger, or Sidekiq.
Standard Rails applications
Put in config/initializers/unleash.rb:
For config.instance_id use a string with a unique identification for the running instance. For example, it could be the hostname if you only run one App per host, or the docker container ID, if you are running in Docker.
If not set, the client will generate a unique UUID for each execution.
To have it available in the rails console command as well, also add to the file above:
Puma in clustered mode
When using Puma with multiple workers configured in puma.rb:
With preload_app!
Then you may keep the client configuration still in config/initializers/unleash.rb:
But you must ensure that the Unleash client is instantiated only after the process is forked.
This is done by creating the client inside the on_worker_boot code block in puma.rb as below:
Without preload_app!
By not using preload_app!:
- The
Railsconstant will not be available. - Phased restarts will be possible.
You need to ensure that in puma.rb:
- The Unleash SDK is loaded with
require 'unleash'explicitly, as it will not be pre-loaded. - All parameters are set explicitly in the
on_worker_bootblock, asconfig/initializers/unleash.rbis not read. - There are no references to
Railsconstant, as that is not yet available.
Example for puma.rb:
Note that we also added shutdown hooks in on_worker_shutdown, to ensure a clean shutdown.
Phusion Passenger
When using Phusion Passenger, the Unleash client needs to be configured and instantiated inside the PhusionPassenger.on_event(:starting_worker_process) code block due to smart spawning:
The initializer in config/initializers/unleash.rb should look like:
Sidekiq
When using Sidekiq, the code block for Unleash.configure must be set beforehand.
For example in config/initializers/unleash.rb.
Unleash context
Add the following method and callback in the application controller to have @unleash_context set for all requests:
Add in app/controllers/application_controller.rb:
Alternatively, you can add this method only to the controllers that use Unleash.
Example usage
Then wherever in your application that you need a feature toggle, you can use:
or if client is set in Rails.configuration.unleash:
If you don’t want to check a feature is disabled with unless, you can also use is_disabled?:
If the feature is not found in the server, it will by default return false.
However, you can override that by setting the default return value to true:
Another possibility is to send a block, Lambda or Proc to evaluate the default value:
or
Note:
- The block/lambda/proc can use the feature name and context as arguments.
- The client will evaluate the fallback function once per call of
is_enabled(). Please keep this in mind when creating your fallback function. - The returned value of the block should be a boolean.
However, the client will coerce the result to a boolean via
!!. - If both a
default_valueandfallback_functionare supplied, the client will define the default value byORing the default value and the output of the fallback function.
Alternatively by using if_enabled (or if_disabled) you can send a code block to be executed as a parameter:
Note: if_enabled (and if_disabled) only support default_value, but not fallback_function.
Check variants
If no flag is found in the server, use the fallback variant.
Bootstrap
Bootstrap configuration allows the client to be initialized with a predefined set of toggle states. Bootstrapping can be configured by providing a bootstrap configuration when initializing the client.
The Bootstrap::Configuration initializer takes a hash with one of the following options specified:
file_path- An absolute or relative path to a file containing a JSON string of the response body from the Unleash server. This can also be set through theUNLEASH_BOOTSTRAP_FILEenvironment variable.url- A url pointing to an Unleash server’s features endpoint, the code sample above is illustrative. This can also be set through theUNLEASH_BOOTSTRAP_URLenvironment variable.url_headers- Headers for the GET HTTP request to theurlabove. Only used if theurlparameter is also set. If this option isn’t set then the bootstrapper will use the same url headers as the Unleash client.data- A raw JSON string as returned by the Unleash server.block- A lambda containing custom logic if you need it, an example is provided below.
You should only specify one type of bootstrapping since only one will be invoked and the others will be ignored. The order of preference is as follows:
- Select a data bootstrapper if it exists.
- If no data bootstrapper exists, select the block bootstrapper.
- If no block bootstrapper exists, select the file bootstrapper from either parameters or the specified environment variable.
- If no file bootstrapper exists, then check for a URL bootstrapper from either the parameters or the specified environment variable.
Example usage:
First, save the toggles locally:
Then use them on startup:
This example could be easily achieved with a file bootstrapper, this is just to illustrate the usage of custom bootstrapping. Be aware that the client initializer will block until bootstrapping is complete.
Client methods
For the full method signatures, see client.rb.
Built-in activation strategies
This client comes with all the required strategies out of the box:
- ApplicationHostnameStrategy
- DefaultStrategy
- FlexibleRolloutStrategy
- GradualRolloutRandomStrategy
- GradualRolloutSessionIdStrategy
- GradualRolloutUserIdStrategy
- RemoteAddressStrategy
- UserWithIdStrategy
Custom strategies
You can add custom activation strategies using configuration.
In order for the strategy to work correctly it should support two methods name and is_enabled?.
Check out our guide for more information on how to build and scale feature flag systems.
Migrating to v6
If you use custom strategies or override built-in ones, read the complete v6 migration guide before upgrading.