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

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

Returns:

  • (Boolean)

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_stderrObject

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_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

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

#suppress(*exception_classes) ⇒ Object


28
29
30
31
32
33
# File 'lib/active_support/core_ext/object_and_class.rb', line 28

def suppress(*exception_classes)
  begin yield
  rescue Exception => e
    raise unless exception_classes.any? {|cls| e.kind_of? cls}
  end
end