Module: Kernel

Defined in:
lib/kernel_extensions.rb

Overview

Removes the ubiquitous and long-winded requiring. So this,

<tt>require File.join(File.dirname(__FILE__), '..', 'lib', 'file_under_test')</tt>

becomes,

<tt>require '../lib/file_under_test/'</tt>

require will accept the following:

* A fully qualified path -- '/Users/elvis/projects/next_best_thing/main.rb'
* A relative path -- '../lib/file_under_test'
* A file local to the current directory -- 'test_helper'

Plus, we try to run File.expand_path on all paths given to require so that files will not accidentally be re-required just because their relative paths were slightly different.

Instance Method Summary collapse

Instance Method Details

#relative_require(path) ⇒ Object



32
33
34
35
36
# File 'lib/kernel_extensions.rb', line 32

def relative_require(path)
  calling_file = caller[1].sub(/\.rb:\d+/, '.rb')
  full_path = File.expand_path(File.join(File.dirname(calling_file), File.split_path(path)))
  require_without_lookup(full_path)
end

#relative_require_wrapper(path) ⇒ Object

Wrapper function to ensure that the caller stack is correct.



39
40
41
# File 'lib/kernel_extensions.rb', line 39

def relative_require_wrapper(path) # :nodoc:
  relative_require(path)
end

#require_with_lookup(path) ⇒ Object Also known as: require



21
22
23
24
25
# File 'lib/kernel_extensions.rb', line 21

def require_with_lookup(path)
  require_without_lookup(path)
rescue LoadError
  relative_require(path)
end