What replaces the core silence (capture) method in Rails 5?

I use the silence method in my spec ( http://apidock.com/rails/Kernel/capture ).

For example, I want to avoid displaying on this block:

silence(:stdout) do ActiveRecord::Tasks::DatabaseTasks.purge_current ActiveRecord::Tasks::DatabaseTasks.load_schema_current end 

It works well, but it is marked as deprecated with Rails 4. Since it will be removed in the next release, I am looking for a replacement but have not found.

Is there something thread safe? Is there a replacement?

+5
source share
1 answer

Nothing replaced. Noting that this is not thread safe, here is what I now have in 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, eg <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 

It is inelegant, but effective.

+2
source

Source: https://habr.com/ru/post/1234539/


All Articles