Module: Kernel

Defined in:
lib/logging/utils.rb

Overview


Instance Method Summary collapse

Instance Method Details

#require!(string, *args) ⇒ Object

call-seq:

require!( string )
require!( string, gem_version )
require!( string, gem_name, gem_version )

Attempt to the load the library named string using the standard Kernel#require method. If the library cannot be loaded then require rubygems and retry the original require of the library.

Raises a LoadError if the library cannot be loaded.

If a gem_version is given, then the rubygems gem command is used to load the specific version of the gem. The library string is used for the gem_name if one is omitted.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/logging/utils.rb', line 136

def require!( string, *args )
  return require(string) if args.empty?

  name, version = *args
  version, name = name, string if name =~ %r/^[0-9<>=~]/
  version ||= '> 0'

  gem name, version
  require(string)
rescue LoadError, NoMethodError
  retry if $use_rubygems and require('rubygems')
  if $whiny_require
    name ||= string
    $stderr.puts "Required library #{string.inspect} could not be loaded."
    $stderr.puts "Try:\tgem install #{name}"
  end
  raise
end

#require?(string, *args) ⇒ Boolean

call-seq:

require?( string )
require?( string, gem_version )
require?( string, gem_name, gem_version )

Attempt to the load the library named string using the standard Kernel#require method. If the library cannot be loaded then require rubygems and retry the original require of the library.

Returns true if the library was successfully loaded. Returns false if the library could not be loaded. This method will never raise an exception.

If a gem_version is given, then the rubygems gem command is used to load the specific version of the gem. The library string is used for the gem_name if one is omitted.

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
# File 'lib/logging/utils.rb', line 172

def require?( string, *args )
  wr, $whiny_require = $whiny_require, false
  require!(string, *args)
  return true
rescue LoadError
  return false
ensure
  $whiny_require = wr
end