Skip to content

Commit ac545e2

Browse files
committed
Make Kernel#silence support :verbose, :warnings and :debug
The docs listed these, but silence only mapped :stderr and :stdout to streams and passed every other symbol through as a stream, so silence(:verbose) raised NoMethodError (undefined method 'reopen' for Symbol). :verbose and :warnings now set $VERBOSE to nil, and :debug sets $DEBUG to false, for the duration of the block. Both are restored afterwards, even if the block raises. Streams work as before. Replaces the empty silence test with tests for the flags, restoring after a raise, and the return value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2Njj4j1r9twkVcXsMySLW
1 parent d5b0d36 commit ac545e2

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎HISTORY.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Changes:
4141
* `require 'facets'` no longer crashes on Ruby 4.1, which removed
4242
`ObjectSpace._id2ref`. `ObjectSpace[]` is only defined where `_id2ref`
4343
exists.
44+
* `Kernel#silence` now supports `:verbose`/`:warnings` and `:debug`, as its
45+
docs always said, instead of raising `NoMethodError`. They set `$VERBOSE`
46+
to nil and `$DEBUG` to false within the block.
4447

4548
* Internal
4649

‎lib/core/facets/kernel/silence.rb‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@ module Kernel
1010
# and +warnings+, which is the same as +verbose+. You can also
1111
# use the actual streams, STDERR and STDOUT.
1212
#
13-
# Silencing works by reopening the stream on the null device, so it
13+
# silence(:verbose) do
14+
# warn "won't see me either"
15+
# end
16+
#
17+
# +verbose+ sets $VERBOSE to nil and +debug+ sets $DEBUG to false
18+
# for the duration of the block.
19+
#
20+
# Silencing a stream works by reopening it on the null device, so it
1421
# affects the whole process, not just the current thread.
1522
def silence(*streams) #:yield:
16-
streams = streams.map do |stream|
17-
case stream
18-
when :stderr
19-
STDERR
20-
when :stdout
21-
STDOUT
22-
else
23-
stream
24-
end
25-
end
23+
verbose, debug = $VERBOSE, $DEBUG
24+
$VERBOSE = nil if (streams & [:verbose, :warnings]).any?
25+
$DEBUG = false if streams.include?(:debug)
2626

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

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

4144
# Just like #silence, but will default to

‎test/core/kernel/test_silence.rb‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@
77
concern "Not straight foward to test since they effect output."
88

99
method :silence do
10+
11+
test "verbose and warnings unset $VERBOSE within the block" do
12+
silence(:verbose){ $VERBOSE }.assert == nil
13+
silence(:warnings){ $VERBOSE }.assert == nil
14+
end
15+
16+
test "debug unsets $DEBUG within the block" do
17+
debug = $DEBUG
18+
begin
19+
$DEBUG = true
20+
silence(:debug){ $DEBUG }.assert == false
21+
$DEBUG.assert == true
22+
ensure
23+
$DEBUG = debug
24+
end
25+
end
26+
27+
test "restores $VERBOSE even if the block raises" do
28+
verbose = $VERBOSE
29+
begin
30+
silence(:verbose){ raise "boom" }
31+
rescue RuntimeError
32+
end
33+
$VERBOSE.assert == verbose
34+
end
35+
36+
test "returns the block's value" do
37+
silence{ 42 }.assert == 42
38+
end
39+
1040
end
1141

1242
method :silence_stream do

0 commit comments

Comments
 (0)