Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Changes:
* `require 'facets'` no longer crashes on Ruby 4.1, which removed
`ObjectSpace._id2ref`. `ObjectSpace[]` is only defined where `_id2ref`
exists.
* `Kernel#silence` now supports `:verbose`/`:warnings` and `:debug`, as its
docs always said, instead of raising `NoMethodError`. They set `$VERBOSE`
to nil and `$DEBUG` to false within the block.

* Internal

Expand Down
27 changes: 15 additions & 12 deletions lib/core/facets/kernel/silence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ module Kernel
# and +warnings+, which is the same as +verbose+. You can also
# use the actual streams, STDERR and STDOUT.
#
# Silencing works by reopening the stream on the null device, so it
# silence(:verbose) do
# warn "won't see me either"
# end
#
# +verbose+ sets $VERBOSE to nil and +debug+ sets $DEBUG to false
# for the duration of the block.
#
# Silencing a stream works by reopening it on the null device, so it
# affects the whole process, not just the current thread.
def silence(*streams) #:yield:
streams = streams.map do |stream|
case stream
when :stderr
STDERR
when :stdout
STDOUT
else
stream
end
end
verbose, debug = $VERBOSE, $DEBUG
$VERBOSE = nil if (streams & [:verbose, :warnings]).any?
$DEBUG = false if streams.include?(:debug)

return yield if streams.empty?
streams = (streams - [:verbose, :warnings, :debug]).map do |stream|
{stderr: STDERR, stdout: STDOUT}.fetch(stream, stream)
end

on_hold = streams.collect{ |stream| stream.dup }
streams.each do |stream|
Expand All @@ -36,6 +38,7 @@ def silence(*streams) #:yield:
streams.each_with_index do |stream, i|
stream.reopen(on_hold[i])
end if on_hold
$VERBOSE, $DEBUG = verbose, debug
end

# Just like #silence, but will default to
Expand Down
30 changes: 30 additions & 0 deletions test/core/kernel/test_silence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
concern "Not straight foward to test since they effect output."

method :silence do

test "verbose and warnings unset $VERBOSE within the block" do
silence(:verbose){ $VERBOSE }.assert == nil
silence(:warnings){ $VERBOSE }.assert == nil
end

test "debug unsets $DEBUG within the block" do
debug = $DEBUG
begin
$DEBUG = true
silence(:debug){ $DEBUG }.assert == false
$DEBUG.assert == true
ensure
$DEBUG = debug
end
end

test "restores $VERBOSE even if the block raises" do
verbose = $VERBOSE
begin
silence(:verbose){ raise "boom" }
rescue RuntimeError
end
$VERBOSE.assert == verbose
end

test "returns the block's value" do
silence{ 42 }.assert == 42
end

end

method :silence_stream do
Expand Down
Loading