Project

dirge

0.0
No commit activity in last 3 years
No release in over 3 years
Relative require, relative autoload and __DIR__
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0.0.2
 Project Readme

Dirge¶ ↑

DEPRECATED¶ ↑

This library is no longer maintained. Everything it does and so much more is now provided by github.com/joshbuddy/callsite. To get back Dirge’s specific functionality, you can do the following

gem install 'callsite'

And in ruby

require 'dirge'

Now everything below will work as-is!

Usage¶ ↑

require 'dirge'
require_relative '../to_my_file'

Or, the much simpler notation

require ~'../to_my_file'

annnd.

class MyAwesomeClass
  autoload_relative :MyConstant, 'my_awesome_class/my_constant'
end

Again, this could be

class MyAwesomeClass
  autoload :MyConstant, ~'my_awesome_class/my_constant'
end

All of this is powered by the new function:

__DIR__

which takes a single line off of Kernel.caller and give you back the relative path. Calling __DIR__(caller.first) for instance, will give you the relative path to whoever called you.

There is also a more explicit, and probably, more readable version.

File.relative(path)

This will convert your path into a relative directory from wherever you are.

require_relative 'to/my/file'

and

require File.relative('to/my/file')

should be identical.