Module: Kernel

Defined in:
activesupport/lib/active_support/core_ext/kernel/concern.rb,
activesupport/lib/active_support/core_ext/kernel/reporting.rb,
activesupport/lib/active_support/core_ext/kernel/singleton_class.rb

Instance Method Summary collapse

Instance Method Details

#class_eval(*args, &block) ⇒ Object

class_eval on an object acts like singleton_class.class_eval.



3
4
5
# 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

#concern(topic, &module_definition) ⇒ Object

A shortcut to define a toplevel concern, not within a module.

See Module::Concerning for more.



7
8
9
# File 'activesupport/lib/active_support/core_ext/kernel/concern.rb', line 7

def concern(topic, &module_definition)
  Object.concern topic, &module_definition
end

#enable_warningsObject

Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.



19
20
21
# File 'activesupport/lib/active_support/core_ext/kernel/reporting.rb', line 19

def enable_warnings
  with_warnings(true) { yield }
end

#silence_warningsObject

Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.

silence_warnings do
  value = noisy_call # no warning voiced
end

noisy_call # warning voiced


13
14
15
# File 'activesupport/lib/active_support/core_ext/kernel/reporting.rb', line 13

def silence_warnings
  with_warnings(nil) { yield }
end

#suppress(*exception_classes) ⇒ Object

Blocks and ignores any exception passed as argument if raised within the block.

suppress(ZeroDivisionError) do
  1/0
  puts 'This code is NOT reached'
end

puts 'This code gets executed and nothing related to ZeroDivisionError was seen'


40
41
42
43
# File 'activesupport/lib/active_support/core_ext/kernel/reporting.rb', line 40

def suppress(*exception_classes)
  yield
rescue *exception_classes
end

#with_warnings(flag) ⇒ Object

Sets $VERBOSE for the duration of the block and back to its original value afterwards.



25
26
27
28
29
30
# File 'activesupport/lib/active_support/core_ext/kernel/reporting.rb', line 25

def with_warnings(flag)
  old_verbose, $VERBOSE = $VERBOSE, flag
  yield
ensure
  $VERBOSE = old_verbose
end