модуль Kernel
Открытые методы экземпляра
# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 89
def capture(stream)
ActiveSupport::Deprecation.warn(
"`#capture(stream)` is deprecated and will be removed in the next release."
) #not thread-safe
stream = stream.to_s
captured_stream = Tempfile.new(stream)
stream_io = eval("$#{stream}")
origin_stream = stream_io.dup
stream_io.reopen(captured_stream)
yield
stream_io.rewind
return captured_stream.read
ensure
captured_stream.close
captured_stream.unlink
stream_io.reopen(origin_stream)
end Захватывает указанный поток и возвращает его:
stream = capture(:stdout) { puts 'notice' }
stream # => "notice\n"
stream = capture(:stderr) { warn 'error' }
stream # => "error\n"
даже для дочерних процессов:
stream = capture(:stdout) { system('echo notice') }
stream # => "notice\n"
stream = capture(:stderr) { system('echo error 1>&2') }
stream # => "error\n"
# File activesupport/lib/active_support/core_ext/kernel/singleton_class.rb, line 3 def class_eval(*args, &block) singleton_class.class_eval(*args, &block) end
#class_eval для объекта ведет себя как singleton_class.class_eval.
# File activesupport/lib/active_support/core_ext/kernel/concern.rb, line 7 def concern(topic, &module_definition) Object.concern topic, &module_definition end
Сокращение для определения общего concern, не внутри модуля.
См. Module::Concerning для более подробной информации.
# File activesupport/lib/active_support/core_ext/kernel/debugger.rb, line 4 def debugger message = "\n***** Debugger requested, but was not available (ensure the debugger gem is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n" defined?(Rails.logger) ? Rails.logger.info(message) : $stderr.puts(message) end
Запускает сеанс отладки, если gem debugger загружен (вызовите rails server –debugger для загрузки).
# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 20
def enable_warnings
with_warnings(true) { yield }
end Устанавливает $VERBOSE в true на время блока и возвращает его к исходному значению после.
# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 115
def quietly
ActiveSupport::Deprecation.warn(
"`#quietly` is deprecated and will be removed in the next release."
) #not thread-safe
silence_stream(STDOUT) do
silence_stream(STDERR) do
yield
end
end
end Отключает вывод STDOUT и STDERR, даже для дочерних процессов.
quietly { system 'bundle install' }
Этот метод не потокобезопасен.
# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 51 def silence_stream(stream) old_stream = stream.dup stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null') stream.sync = true yield ensure stream.reopen(old_stream) old_stream.close end
Устаревший метод: этот метод не потокобезопасен. Отключает любой поток на время блока.
silence_stream(STDOUT) do puts 'This will never be seen' end puts 'But this will'
Этот метод не потокобезопасен.
# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 14
def silence_warnings
with_warnings(nil) { yield }
end Устанавливает $VERBOSE в nil на время блока и возвращает его к исходному значению после.
silence_warnings do value = noisy_call # no warning voiced end noisy_call # warning voiced
# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 69 def suppress(*exception_classes) yield rescue *exception_classes end
Блокирует и игнорирует любые исключения, переданные в качестве аргумента, если они возникают внутри блока.
suppress(ZeroDivisionError) do 1/0 puts 'This code is NOT reached' end puts 'This code gets executed and nothing related to ZeroDivisionError was seen'
# File activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 26 def with_warnings(flag) old_verbose, $VERBOSE = $VERBOSE, flag yield ensure $VERBOSE = old_verbose end
Устанавливает $VERBOSE на время блока и возвращает его к исходному значению после.
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.