RSpec::Support

Public Class Methods

define_optimized_require_for_rspec(lib, &require_relative) click to toggle source

@api private

Defines a helper method that is optimized to require files from the named lib. The passed block MUST be `{ |f| require_relative f }` because for `require_relative` to work properly from within the named lib the line of code must be IN that lib.

`require_relative` is preferred when available because it is always O(1), regardless of the number of dirs in $LOAD_PATH. `require`, on the other hand, does a linear O(N) search over the dirs in the $LOAD_PATH until it can resolve the file relative to one of the dirs.

# File lib/rspec/support.rb, line 14
def self.define_optimized_require_for_rspec(lib, &require_relative)
  name = "require_rspec_#{lib}"

  if Kernel.respond_to?(:require_relative)
    (class << self; self; end).__send__(:define_method, name) do |f|
      require_relative.call("#{lib}/#{f}")
    end
  else
    (class << self; self; end).__send__(:define_method, name) do |f|
      require "rspec/#{lib}/#{f}"
    end
  end
end
method_handle_for(object, method_name) click to toggle source
# File lib/rspec/support.rb, line 47
def self.method_handle_for(object, method_name)
  KERNEL_METHOD_METHOD.bind(object).call(method_name)
rescue NameError => original
  begin
    handle = object.method(method_name)
    raise original unless handle.is_a? Method
    handle
  rescue Exception
    raise original
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.