Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/active_support/dependencies.rb,
lib/active_support/core_ext/kernel.rb,
lib/active_support/core_ext/object_and_class.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#`(command) ⇒ Object
Makes backticks behave (somewhat more) similarly on all platforms.
-
#blank? ⇒ Boolean
“”, “ ”, nil, [], and {} are blank.
- #load(file, *extras) ⇒ Object
- #remove_subclasses_of(*superclasses) ⇒ Object
- #require(file, *extras) ⇒ Object
-
#require_library_or_gem(library_name) ⇒ Object
Method that requires a library, ensuring that rubygems is loaded.
-
#returning(value) ⇒ Object
A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
-
#silence_stderr ⇒ Object
Silences stderr for the duration of the block.
-
#silence_warnings ⇒ Object
Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
- #subclasses_of(*superclasses) ⇒ Object
- #suppress(*exception_classes) ⇒ Object
Instance Method Details
#`(command) ⇒ Object
Makes backticks behave (somewhat more) similarly on all platforms. On win32 ‘nonexistent_command` raises Errno::ENOENT; on Unix, the spawned shell prints a message to stderr and sets $?. We emulate Unix on the former but not the latter.
52 53 54 55 56 |
# File 'lib/active_support/core_ext/kernel.rb', line 52 def `(command) #:nodoc: super rescue Errno::ENOENT => e STDERR.puts "#$0: #{e}" end |
#blank? ⇒ Boolean
“”, “ ”, nil, [], and {} are blank
18 19 20 21 22 23 24 25 26 |
# File 'lib/active_support/core_ext/object_and_class.rb', line 18 def blank? if respond_to?(:empty?) && respond_to?(:strip) empty? or strip.empty? elsif respond_to?(:empty?) empty? else !self end end |
#load(file, *extras) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/active_support/dependencies.rb', line 206 def load(file, *extras) super(file, *extras) rescue Object => exception exception.blame_file! file raise end |
#remove_subclasses_of(*superclasses) ⇒ Object
2 3 4 5 6 |
# File 'lib/active_support/core_ext/object_and_class.rb', line 2 def remove_subclasses_of(*superclasses) subclasses_of(*superclasses).each do |subclass| Object.send(:remove_const, subclass.to_s) rescue nil end end |
#require(file, *extras) ⇒ Object
213 214 215 216 217 218 |
# File 'lib/active_support/dependencies.rb', line 213 def require(file, *extras) super(file, *extras) rescue Object => exception exception.blame_file! file raise end |
#require_library_or_gem(library_name) ⇒ Object
Method that requires a library, ensuring that rubygems is loaded
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_support/core_ext/kernel.rb', line 59 def require_library_or_gem(library_name) begin require library_name rescue LoadError => cannot_require # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try. begin require 'rubygems' rescue LoadError => rubygems_not_installed raise cannot_require end # 2. Rubygems is installed and loaded. Try to load the library again begin require library_name rescue LoadError => gem_not_installed raise cannot_require end end end |
#returning(value) ⇒ Object
A Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
def foo
returning values = [] do
values << 'bar'
values << 'baz'
end
end
foo # => ['bar', 'baz']
13 14 15 16 |
# File 'lib/active_support/core_ext/kernel.rb', line 13 def returning(value) yield value end |
#silence_stderr ⇒ Object
Silences stderr for the duration of the block.
silence_stderr do
$stderr.puts 'This will never be seen'
end
$stderr.puts 'But this will'
39 40 41 42 43 44 45 46 |
# File 'lib/active_support/core_ext/kernel.rb', line 39 def silence_stderr old_stderr = STDERR.dup STDERR.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') STDERR.sync = true yield ensure STDERR.reopen(old_stderr) end |
#silence_warnings ⇒ Object
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
25 26 27 28 29 30 |
# File 'lib/active_support/core_ext/kernel.rb', line 25 def silence_warnings old_verbose, $VERBOSE = $VERBOSE, nil yield ensure $VERBOSE = old_verbose end |
#subclasses_of(*superclasses) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/active_support/core_ext/object_and_class.rb', line 8 def subclasses_of(*superclasses) subclasses = [] ObjectSpace.each_object(Class) do |k| next if (k.ancestors & superclasses).empty? || superclasses.include?(k) || k.to_s.include?("::") || subclasses.include?(k) subclasses << k end subclasses end |