ActiveSupport’s Stream Silencing - Without ActiveSupport

Total Downloads Downloads Today Network Stars Version Build Maintainability Test Coverage Depfu Open Source Helpers Chat License: MIT

SilentStream is an extraction of some parts of ActiveSupport’s Kernel Reporting Core Extentions.

Since July 2014 silence_stream, silence_stderr, capture, silence, and quietly have been deprecated because they are not thread safe. See that discussion in the PR where it all went down. I rely on them a lot in single threaded code, and so I plan to keep them alive. With the exception of silence, which was just an alias of capture.

This gem was taken out of Rails but it is not Rails dependent. The extraction was total (even the tests!), and this is now a pure Ruby library, which can be used in any Ruby project without encumbrances.


One aspect of what this gem provides can be achieved with the Rails’ built-in LoggerSilence, which is thread safe. You will have to decide what is right for you!

Doing a Rails <= 4 to Rails >= 5 Upgrade?

The reason for not keeping silence as it was in Rails 4, i.e. an alias of capture, is that the just mentioned LoggerSilence now uses this term, and it is shipping with Rails 5. I don’t want to make this gem incompatible with Rails 5, so you will have to convert Rails <= 4 implementations that utilize silence over to capture when using this gem. One further point of difference is this gem does not add the methods to Kernel or Object. You can do that if you like via include. By default this gem does not pollute anything, so you will need to include SilentStream in any class using these methods.


Add this line to your application’s Gemfile:

gem 'silent_stream'

And then execute:

$ bundle

Or install it yourself as:

$ gem install silent_stream



class Bogosity
  include SilentStream # allows use at instance or class level

  def silent
    silence_all(true) do
      puts "play that funky music" "git jiggy with it"
  class << self
    def noise
      silence_all(false) do
        puts "play that funky music" "git jiggy with it"

And run

>> # has no output
=> nil
>> Bogosity.noise # is noisy
play that funky music
=> nil

Use in Specs / Tests

Make the methods avaialble:

RSpec.configure do |conf|
  conf.include SilentStream

Then add a test on output:

it 'has output' do
  output = capture(:stdout) { subject.request(:get, '/success') }
  logs = [
      'INFO -- request: GET',
      'INFO -- response: Status 200'
  expect(output).to include(*logs)

See it in practice in the specs for the oauth2 gem


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at

Code of Conduct

Everyone interacting in the AnonymousActiveRecord project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.


This library aims to adhere to [Semantic Versioning 2.0.0][semver]. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.

As a result of this policy, you can (and should) specify a dependency on this gem using the [Pessimistic Version Constraint][pvc] with two digits of precision.

For example:

spec.add_dependency 'silent_stream', '~> 1.0'


  • Copyright (c) 2018 [Peter H. Boling][peterboling] of [Rails Bling][railsbling]