Skip to content

Instantly share code, notes, and snippets.

@searls
Created June 18, 2024 18:00
Show Gist options
  • Save searls/9caa12f66c45a72e379e7bfe4c48405b to your computer and use it in GitHub Desktop.
Save searls/9caa12f66c45a72e379e7bfe4c48405b to your computer and use it in GitHub Desktop.
# This is a stupid utility to squelch stdout output via puts (could do the same
# for warn; could also override stdout entirely)
#
# Usage in a test, after requiring and including it:
#
# setup do
# suppress_puts([
# /Execution context was destroyed, most likely because of a navigation/
# ])
# end
#
# teardown do
# restore_puts
# end
#
module StdoutHelpers
def suppress_puts(only_these_patterns)
@__og_puts = og_puts = Kernel.method(:puts)
Kernel.define_method(:puts) do |*args|
if only_these_patterns.nil?
nil
elsif only_these_patterns.none? { |pattern| args.first.to_s =~ pattern }
og_puts.call(*args)
end
end
end
def restore_puts
Kernel.define_singleton_method(:puts, @__og_puts)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment