Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport | reporting

概要

reporting

詳細

reportingについて

一覧

method 内容
Kernel#enable_warnings $VERBOSEをtrueにしてブロックを実行する。ブロック実行後、$VERBOSEは処理前の値に戻す
Kernel#slice_warnings $VERBOSEをnilにしてブロックを実行する。ブロック実行後、$VERBOSEは処理前の値に戻す
Kernel#with_warnings enable_warnings、slice_warningsが内部で利用している。$VERBOSEを切り替えてブロックを実行する。ブロック実行後、$VERBOSEは処理前の値に戻す
Kernel#silence_stream 指定したストリームの出力を抑制する
Kernel#suppress 指定した例外を抑止する。リスト指定も可能
Kernel#quietly 標準出力・標準エラーの双方を抑止
Kernel#capture ブロックの処理中のストリーム出力の内容を取得する

サンプル

# encoding: utf-8
require 'active_support/core_ext/kernel/reporting'
require 'tbpgr_utils'

puts_eval 'capture(:stdout) {puts "hoge"}', binding
$VERBOSE = true
HOGE = "hoge"
silence_warnings {HOGE = "hige"} # => $VERBOSEがtrueにも関わらず警告がでない
$VERBOSE = false
enable_warnings {HOGE = "hige"} # => $VERBOSEがfalseにも関わらず警告がでる

$VERBOSE = true
silence_stream(STDOUT) {puts "hoge"} # => ブロック内は標準出力がでない

class HogeError < StandardError
end
suppress(HogeError) do
  raise HogeError.new("hoge") # 抑止されているため例外はスローされない(ただし処理は抜ける)
  puts "after HogeError" # 実行されない
end

quietly {puts "hoge"}

ret = capture(:stdout) { puts "stdout capture" }
puts ret

出力

capture(:stdout) {puts "hoge"} # => "hoge\n"
kernel_reporting.rb:10: warning: already initialized constant HOGE
stdout capture