# Require, from within lib, files located under the local path

**URL:** <https://www.ruby-forum.com/t/require-from-within-lib-files-located-under-the-local-path/185592>\
**Category:** Ruby\
**Created:** [March 23, 2010, 6:37am UTC](https://www.ruby-forum.com/t/require-from-within-lib-files-located-under-the-local-path/185592 "2010-03-23T06:37:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jdv](https://www.ruby-forum.com/u/jdv)\
**Post date:** [March 23, 2010, 6:37am UTC](https://www.ruby-forum.com/t/require-from-within-lib-files-located-under-the-local-path/185592/1 "2010-03-23T06:37:49Z")

</div>

Hi.

Here’s an issue I’ve not been able to solve for some time now.

Let’s say I designed a tiny library, following a standard structure:

$ tree  
.  
|-- LICENSE  
|-- README.md  
|-- Rakefile  
|-- VERSION  
|-- lib  
| |-- mylib  
| | `-- core.rb |`– metamorphosis.rb  
|-- metamorphosis.gemspec  
`-- test

Using Jeweler to build and install the gem (successfully), it goes into  
(for instance): home/YOU/.rvm/gems/ruby-1.9.1-p378/gems/mylib-0.1.0/

The purpose of mylib involves allowing the user who will eventually  
perform a require ‘mylib’, to create, along the file.rb performing the  
require, a folder (let’s call it mylib\_stuff). So the user sets up the  
following structure:

$ tree  
.  
|-- main.rb  
`-- mylib_stuff |-- a_piece_of stuff.rb |-- ...`– another\_piece\_of\_stuff.rb

The core.rb file needs to use what’s inside mylib\_stuff/\*. Using the  
standard technique: require File.expand\_path("…/…/mylib\_stuff",  
**FILE** ), you end up with the following error: no such file to load –  
/home/YOU/.rvm/gems/ruby-1.9.1-p378/gems/mylib-0.1.0/mylib\_stuff/[foobar]

That’s because **FILE** is core.rb. So what I’d like to do is passing  
require a path which would be aware of the location the gem mylib is  
used from, so it’ll access this particular path. Another script using  
mylib would trigger different require statements in core.rb, involving  
it’s very own mylib\_stuff local directory.

Thank you!

---

<div class="post-metadata">

**Author:** ![jdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jdv](https://www.ruby-forum.com/u/jdv)\
**Post date:** [March 23, 2010, 6:38am UTC](https://www.ruby-forum.com/t/require-from-within-lib-files-located-under-the-local-path/185592/2 "2010-03-23T06:38:53Z")

</div>

s/along the file.rb/along the main.rb/

---

<div class="post-metadata">

**Author:** ![jdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jdv](https://www.ruby-forum.com/u/jdv)\
**Post date:** [March 23, 2010, 8:10am UTC](https://www.ruby-forum.com/t/require-from-within-lib-files-located-under-the-local-path/185592/3 "2010-03-23T08:10:59Z")

</div>

Ok so here’s what I eventually did:

require ‘pathname’ # I love it 🙂  
module MyLib  
def self.extended base #:nodoc:  
# the receiver is the extended module or class  
@receiver = base

```
# paths of the receiver file and receiver stuff
@base_path = Pathname.new(File.expand_path($0)).dirname
@stuff_path = @base_path + "mylib_stuff"

# ... do whatever with base and the paths

```

end

# …

end

I don’t know if this practice of relying on $0 is solid, though.  
Any hint?

---

<div class="post-metadata">

**Author:** ![jdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jdv](https://www.ruby-forum.com/u/jdv)\
**Post date:** [March 23, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/require-from-within-lib-files-located-under-the-local-path/185592/4 "2010-03-23T14:59:36Z")

</div>

Jean-denis Vauguet wrote:

> I don’t know if this practice of relying on $0 is solid, though.

It’s very risky:

- on many platforms it’s truncated
- the script which the user started running isn’t necessarily the file  
which is requiring your code

I suggest you look at Kernel#caller instead.

Unfortunately it just gives you strings which you have to parse  
yourself, and you’ll probably need to grep out all the rubygems-related  
paths.

For an example, look in the sinatra gem (lib/sinatra/base.rb,  
specifically methods caller\_locations and caller\_files). Sinatra uses  
this to locate the application source code, for reading inline  
templates.

---

<div class="post-metadata">

**Author:** ![jdv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@jdv](https://www.ruby-forum.com/u/jdv)\
**Post date:** [March 24, 2010, 4:49am UTC](https://www.ruby-forum.com/t/require-from-within-lib-files-located-under-the-local-path/185592/5 "2010-03-24T04:49:59Z")

</div>

Brian C. wrote:

> I suggest you look at Kernel#caller instead.
> 
> Unfortunately it just gives you strings which you have to parse  
> yourself, and you’ll probably need to grep out all the rubygems-related  
> paths.
> 
> For an example, look in the sinatra gem (lib/sinatra/base.rb,  
> specifically methods caller\_locations and caller\_files). Sinatra uses  
> this to locate the application source code, for reading inline  
> templates.

Thank you so much 🙂
