Dear Russian friends, please watch President Zelenskyy's speech addressed to you. 🇺🇦Help our brave mates in Ukraine with a donation.


Reader is the simplest effect. It passes a value down to the stack.

require 'dry-effects'

class SetLocaleMiddleware
  include Dry::Effects::Handler.Reader(:locale)

  def initialize(app)
    @app = app

  def call(env)
    with_locale(detect_locale(env)) do

  def detect_locale(env)
    # arbitrary detection logic

### Anywhere in the app

class GreetUser
  include Dry::Effects.Reader(:locale)

  def call(user)
    case locale
    when :en then "Hello #{}"
    when :de then "Hallo #{}"
    when :ru then "Привет, #{}"
    when :it then "Ciao #{}"

Testing with Reader

If you run GreetUser#call without a Reader handler, it will raise an error. For unit tests you'll need some wrapping code:

RSpec.describe GreetUser do
  include Dry::Effects::Handler.Reader(:locale)

  subject(:greet) { }

  let(:user) { double(:user, name: 'John') }

  it 'uses the current locale to greet the user' do
    examples = {
      en: 'Hello John',
      de: 'Hallo John',
      ru: 'Привет, John',
      it: 'Ciao John'

    examples.each do |locale, expected_greeting|
      with_locale(locale) do
        expect(greet.(user)).to eql(expected_greeting)

You can provide locale in an around(:each) hook:

require 'dry/effects'

# Build a provider object with .call interface
locale_provider =, as: :call))

RSpec.configure do |config|
  config.around(:each) do |ex|
    locale_provider.(:en, &ex)

Nesting readers

As a general rule, if there are two handlers in the stack, the nested takes precedence:

require 'dry/effects'

extend Dry::Effects::Handler.Reader(:locale)
extend Dry::Effects.Reader(:locale)

with_locale(:en) { with_locale(:de) { locale } } # => :de

Mixing readers

Every Reader has an identifier. Handlers with different identifiers won't interfere:

require 'dry/effects'

extend Dry::Effects::Handler.Reader(:locale)
extend Dry::Effects::Handler.Reader(:context)
extend Dry::Effects.Reader(:locale)
extend Dry::Effects.Reader(:context)

with_locale(:en) { with_context(:background) { [locale, context] } } # => [:en, :background]
# Order doesn't matter:
with_context(:background) { with_locale(:en) { [locale, context] } } # => [:en, :background]

Relation to State

Reader is part of the State effect.

Tradeoffs of implicit passing

Passing values implicitly is not good or bad by itself; you should consider how it affects your code in every case. Providing the current locale is a good example where reader effect can be justified. On the other hand, passing optional values such as the IP-address of the current user should be done explicitly because they are not always present (consider background jobs, rake tasks, etc.).

octocatEdit on GitHub