# How to get class' filepath from methods in included mixin?

**URL:** <https://www.ruby-forum.com/t/how-to-get-class-filepath-from-methods-in-included-mixin/167364>\
**Category:** Ruby\
**Created:** [May 7, 2009, 2:16am UTC](https://www.ruby-forum.com/t/how-to-get-class-filepath-from-methods-in-included-mixin/167364 "2009-05-07T02:16:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 7, 2009, 2:16am UTC](https://www.ruby-forum.com/t/how-to-get-class-filepath-from-methods-in-included-mixin/167364/1 "2009-05-07T02:16:18Z")

</div>

Hi,

File.dirname( **FILE** ) returns the current folder where the script in,  
How to get it from methods in included modules?

My sample code as below:

## helper.rb

module Helper  
def your\_path  
puts instance\_eval { File.expand\_path(File.dirname( **FILE** )) } #  
doesn’t work  
end  
end

## a.rb

require ‘helper/helper’

class A  
include Helper  
end

A.new.your\_path #=\> return helper’s file path, not class A’s

---

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 7, 2009, 2:41am UTC](https://www.ruby-forum.com/t/how-to-get-class-filepath-from-methods-in-included-mixin/167364/2 "2009-05-07T02:41:51Z")

</div>

Nigel Hennan wrote:

> module Helper  
> class A  
> include Helper  
> end
> 
> A.new.your\_path #=\> return helper’s file path, not class A’s

This seems to work:

[~/tmp] cat helper/helper.rb  
module Helper  
def self.included m  
file = caller[1][/(.\*):\d+$/, 1] ## a bit fragile, maybe  
dir = File.expand\_path(File.dirname(file))  
m.const\_set “YourPath”, dir  
end

def your\_path  
self.class::YourPath  
end  
end  
[~/tmp] cat a.rb  
require ‘helper/helper’

class A  
include Helper  
end

p A.new.your\_path  
[~/tmp] ruby a.rb  
“/home/vjoel/tmp”

---

<div class="post-metadata">

**Author:** ![nigelh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ce73a5/32.png) [@nigelh](https://www.ruby-forum.com/u/nigelh)\
**Post date:** [May 7, 2009, 3:08am UTC](https://www.ruby-forum.com/t/how-to-get-class-filepath-from-methods-in-included-mixin/167364/3 "2009-05-07T03:08:22Z")

</div>

Joel VanderWerf wrote:

> This seems to work:

Yes, Thanks Joel!

Nigel
