# Detect the path used to require a file?

**URL:** <https://www.ruby-forum.com/t/detect-the-path-used-to-require-a-file/179634>\
**Category:** Ruby\
**Created:** [November 30, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/detect-the-path-used-to-require-a-file/179634 "2009-11-30T22:28:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [November 30, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/detect-the-path-used-to-require-a-file/179634/1 "2009-11-30T22:28:35Z")

</div>

Hi all,

I’m having a problem in which a file is being required twice, using  
slightly different paths, and I want to track down where this is  
happening. Is there a way I can print out the path used to require a  
given file?

Thanks!

Brian

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [November 30, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/detect-the-path-used-to-require-a-file/179634/2 "2009-11-30T22:32:15Z")

</div>

On Nov 30, 4:28 pm, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I’m having a problem in which a file is being required twice, using  
> slightly different paths, and I want to track down where this is  
> happening. Is there a way I can print out the path used to require a  
> given file?

Create a load monitor, eg.

alias old\_require require  
def require(path)  
puts path, caller, “—”  
old\_require(path)  
end

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [December 1, 2009, 9:54am UTC](https://www.ruby-forum.com/t/detect-the-path-used-to-require-a-file/179634/3 "2009-12-01T09:54:42Z")

</div>

> I’m having a problem in which a file is being required twice, using  
> slightly different paths, and I want to track down where this is  
> happening. Is there a way I can print out the path used to require a  
> given file?

In most Ruby implementations the **FILE** constant will be different  
depending on how the file was loaded. Notably, it will reflect the path  
used to load the file.

# == test.rb == puts **FILE**

> ruby -r test.rb -e ‘’  
> ./test.rb

> ruby -r /home/user/temp/test.rb  
> /home/user/temp/test.rb

regards,  
kaspar
