Feature Flagging in Ruby with the Flipper Gem

February 7, 2025

Feature flagging is an essential technique for modern software development, allowing teams to deploy features safely, conduct A/B testing, and enable gradual rollouts without redeploying code. In the Ruby ecosystem, one of the most powerful and flexible libraries for feature flagging is Flipper.


🚀 Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>

🚀 Need Expert Ruby on Rails Developers to Elevate Your Project?

Why Use Feature Flags?

Feature flags (also known as feature toggles) provide several benefits:

  • Gradual Rollouts: Release features to a small percentage of users before a full launch.
  • A/B Testing: Test different versions of a feature with different user groups.
  • Instant Rollback: Disable a problematic feature instantly without deploying new code.
  • User-Specific Access: Enable features only for certain users, such as beta testers or administrators.

Flipper makes all of this easy with a robust API and multiple storage options.


Getting Started with Flipper

Installation

To use Flipper in your Ruby or Rails project, add it to your Gemfile:

# Gemfile
gem 'flipper' 

Then run:

bundle install 

If you’re using Rails, you may also want the Flipper UI for a web-based dashboard:

gem 'flipper-ui' 

And mount it in your routes.rb:

# config/routes.rb
mount Flipper::UI.app(Flipper) => '/flipper' 

Now, you can manage feature flags via a user-friendly web interface!


How to Use Flipper

Flipper offers multiple ways to control feature access. Here are some common use cases:

1. Enabling and Disabling Features Globally

require 'flipper'

flipper = Flipper.new(Flipper::Adapters::Memory.new)

flipper[:new_dashboard].enable   # Enable the feature
flipper[:new_dashboard].disable  # Disable the feature 

2. Rolling Out Features Gradually

You can roll out features by percentage of actors or time:

flipper[:new_dashboard].enable_percentage_of_actors(50) # Enable for 50% of users
flipper[:new_dashboard].enable_percentage_of_time(30)   # Enable 30% of the time 

3. Enabling Features for Specific Users

user = User.find(1)
flipper[:new_dashboard].enable_actor(user)
flipper[:new_dashboard].enabled?(user) # => true 

4. Using Groups to Manage Access

Define groups dynamically:

Flipper.register(:admins) do |user|
  user.admin?
end

flipper[:new_dashboard].enable_group(:admins) 

Now, all admins will have access to the feature.


Choosing a Storage Adapter

By default, Flipper uses in-memory storage, but it supports various backends:

  • ActiveRecord (PostgreSQL, MySQL, SQLite, etc.)
  • Redis
  • Sequel
  • MongoDB

Example with ActiveRecord:

bundle add flipper-active_record
rails generate flipper:active_record
rails db:migrate 
require 'flipper/adapters/active_record'
Flipper.configure do |config|
  config.adapter { Flipper::Adapters::ActiveRecord.new }
end 

Now, Flipper will persist feature flags in your database.


Final Thoughts

The Flipper gem is an incredibly powerful tool for managing feature flags in Ruby applications. Whether you’re rolling out a new feature, running an experiment, or ensuring seamless rollbacks, Flipper gives you the flexibility and control you need.

If you’re working on a Ruby or Rails project and haven’t tried Flipper yet, it’s definitely worth integrating. Your deployments will be safer, and your users will get a smoother experience.

What are your thoughts on feature flagging? Have you used Flipper or another tool? Let’s discuss in the comments! 🚀

Leave a comment