在Rails 5中用什么代替沉默(捕获)内核方法?

Nar*_*emy 5 ruby-on-rails

我在我的规范中使用沉默方法(http://apidock.com/rails/Kernel/capture).

例如,我想避免在这个块上显示:

silence(:stdout) do
  ActiveRecord::Tasks::DatabaseTasks.purge_current
  ActiveRecord::Tasks::DatabaseTasks.load_schema_current
end
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但是自从Rails 4以来它被标记为已弃用.因为它将在下一个版本中删除,我搜索替换但未找到.

是否存在线程安全的东西? 是否存在替代品?

And*_*son 4

没有什么可以取代它。请注意,这不是线程安全的,这是我现在拥有的内容spec_helper.rb

# Silence +STDOUT+ temporarily.
#
# &block:: Block of code to call while +STDOUT+ is disabled.
#
def spec_helper_silence_stdout( &block )
  spec_helper_silence_stream( $stdout, &block )
end

# Back-end to #spec_helper_silence_stdout; silences arbitrary streams.
#
# +stream+:: The output stream to silence, e.g. <tt>$stdout</tt>
# &block::   Block of code to call while output stream is disabled.
#
def spec_helper_silence_stream( stream, &block )
  begin
    old_stream = stream.dup
    stream.reopen( File::NULL )
    stream.sync = true

    yield

  ensure
    stream.reopen( old_stream )
    old_stream.close

  end
end
Run Code Online (Sandbox Code Playgroud)

它不优雅但有效。