Generated banner for Feature Flags Ruby
Generated logo for Feature Flags Ruby

Feature Flags Ruby

PostHogOfficialCreated by PostHog

  • Skill
  • Tool integration
  • Data and AI
  • MIT
  • No issues found

From the author

PostHog feature flags for Ruby applications
— the description by PostHog

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides guidance and best practices for implementing PostHog feature flags within Ruby applications. It covers installation, initialization, and proper usage patterns to ensure efficient flag evaluation, including server-side considerations and cleanup procedures to prevent event loss.

What it does

  • Provides instructions for installing the posthog-ruby gem.
  • Outlines proper client initialization and shutdown procedures for Ruby scripts.
  • Details how to implement boolean and multivariate feature flags.
  • Offers best practices for flag naming, targeting, and performance optimization.
  • Explains how to use environment variables for secure API key management.

When to use it

  • When you need to perform controlled feature rollouts in a Ruby environment.
  • When you want to manage feature flags server-side to avoid UI flickering.
  • When building CLI tools or backend services that require feature toggling.
  • When you need to ensure consistent flag evaluation across user sessions.

Advantages

  • Enables safer feature releases through controlled rollouts.
  • Supports server-side evaluation for improved performance.
  • Provides clear patterns for resource management in Ruby applications.
  • Promotes clean code practices through centralized flag handling.

Usage examples

“How do I initialize the PostHog client in a Ruby script?”
The agent will provide the code to initialize PostHog::Client using environment variables and explain the necessity of calling client.shutdown in an ensure block.
“What is the best way to use feature flags in Ruby to avoid cluttering my code?”
The agent will recommend wrapping flag checks in a single function or method to maintain clean, maintainable code.

Install

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/posthog/feature-flags-ruby/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip feature-flags-ruby.zip -d .agents/skills

Documentation by PostHog

PostHog feature flags for Ruby

This skill helps you add PostHog feature flags to Ruby applications.

Reference files

  • references/ruby.md - Ruby feature flags installation - docs

  • references/adding-feature-flag-code.md - Adding feature flag code - docs

  • references/best-practices.md - Feature flag best practices - docs

Consult the documentation for API details and framework-specific patterns.

Key principles

  • Environment variables: Always use environment variables for PostHog keys. Never hardcode them.

  • Minimal changes: Add feature flag code alongside existing logic. Don't replace or restructure existing code.

  • Boolean flags first: Default to boolean flag checks unless the user specifically asks for multivariate flags.

  • Server-side when possible: Prefer server-side flag evaluation to avoid UI flicker.

PostHog MCP tools

Check if a PostHog MCP server is connected. If available, look for tools related to feature flag management (creating, listing, updating, deleting flags). Use these tools to manage flags directly in PostHog rather than requiring the user to do it manually in the dashboard.

Framework guidelines

  • posthog-ruby is the Ruby SDK gem name (add gem 'posthog-ruby' to Gemfile) but require it with require 'posthog' (NOT require 'posthog-ruby')

  • Use PostHog::Client.new(api_key: key, host: host) for instance-based initialization in scripts and CLIs

  • In CLIs and scripts: MUST call client.shutdown before exit or all events are lost

  • Use begin/rescue/ensure with shutdown in the ensure block for proper cleanup

  • capture and identify take a single hash argument: client.capture(distinct_id: 'user_123', event: 'my_event', properties: { key: 'value' })

  • capture_exception takes POSITIONAL args (not keyword): client.capture_exception(exception, distinct_id, additional_properties) — do NOT use distinct_id: keyword syntax

Files

references/ruby.md

Ruby feature flags installation - Docs

  1. 1

    Install the gem

    Required

    Add the PostHog Ruby gem to your Gemfile:

    Gemfile

    PostHog AI

    gem "posthog-ruby"
  2. 2

    Configure PostHog

    Required

    Initialize the PostHog client with your project token and host:

    Ruby

    PostHog AI

    require 'posthog'
    posthog = PostHog::Client.new({
        api_key: "<ph_project_token>",
        host: "https://us.i.posthog.com",
        on_error: Proc.new { |status, msg| print msg }
    })
  3. 3

    Send events

    Recommended

    Once installed, you can manually send events to test your integration:

    Ruby

    PostHog AI

    posthog.capture({
        distinct_id: 'user_123',
        event: 'button_clicked',
        properties: {
            button_name: 'signup'
        }
    })
  4. 4

    Evaluate boolean feature flags

    Required

    Check if a feature flag is enabled:

    is_my_flag_enabled = posthog.is_feature_enabled('flag-key', 'distinct_id_of_your_user')
    if is_my_flag_enabled
        # Do something differently for this user
        # Optional: fetch the payload
        matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
    end
  5. 5

    Evaluate multivariate feature flags

    Optional

    For multivariate flags, check which variant the user has been assigned:

    enabled_variant = posthog.get_feature_flag('flag-key', 'distinct_id_of_your_user')
    if enabled_variant == 'variant-key' # replace 'variant-key' with the key of your variant
        # Do something differently for this user
        # Optional: fetch the payload
        matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
    end
  6. 6

    Include feature flag information in events

    Required

    If you want to use your feature flag to breakdown or filter events in your insights, you'll need to include feature flag information in those events. This ensures that the feature flag value is attributed correctly to the event.

    Note: This step is only required for events captured using our server-side SDKs or API.

    Set send_feature_flags to true in your capture call:

    Ruby

    PostHog AI

    posthog.capture({
        distinct_id: 'distinct_id_of_your_user',
        event: 'event_name',
        send_feature_flags: true,
    })

    Include $feature property

    Include the $feature/feature_flag_name property in your event properties:

    Ruby

    PostHog AI

    posthog.capture({
        distinct_id: 'distinct_id_of_your_user',
        event: 'event_name',
        properties: {
            '$feature/feature-flag-key': 'variant-key', # replace feature-flag-key with your flag key. Replace 'variant-key' with the key of your variant
        }
    })
  7. 7

    Override server properties

    Optional

    Sometimes, you may want to evaluate feature flags using properties that haven't been ingested yet, or were set incorrectly earlier. You can provide properties to evaluate the flag with:

    posthog.get_feature_flag(
        'flag-key',
        'distinct_id_of_the_user',
        person_properties: {
            'property_name': 'value'
        },
        groups: {
            'your_group_type': 'your_group_id',
            'another_group_type': 'your_group_id',
        },
        group_properties: {
            'your_group_type': {
                'group_property_name': 'value'
            },
            'another_group_type': {
                'group_property_name': 'value'
            },
        },
    )
  8. 8

    Running experiments

    Optional

    Experiments run on top of our feature flags. Once you've implemented the flag in your code, you run an experiment by creating a new experiment in the PostHog dashboard.

  9. 9

    Next steps

    Recommended

    Now that you're evaluating flags, continue with the resources below to learn what else Feature Flags enables within the PostHog platform.

    ResourceDescription
    Creating a feature flagHow to create a feature flag in PostHog
    Adding feature flag codeHow to check flags in your code for all platforms
    Framework-specific guidesSetup guides for React Native, Next.js, Flutter, and other frameworks
    How to do a phased rolloutGradually roll out features to minimize risk
    More tutorialsOther real-world examples and use cases
Community questions

Ask a question

Was this page useful?

HelpfulCould be better

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Needs: Needs an MCP server, Needs an API key

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
PostHog
Publisher
PostHog (Official)
Source
PostHog/skills · skills/posthog/feature-flags/skills/ruby · commit a5d30e7
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced
Also distributed in
PostHog/skills

AutoGineer is not affiliated with or endorsed by PostHog or PostHog.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download feature-flags-ruby.zip