[LRUG] Working out the path of a gem from within the gem
Martin Sadler
mtsbtt at googlemail.com
Mon Mar 3 03:44:42 PST 2008
The Reliable Message gem does that I believe.. goes to look:
def initialize file, logger = nil
@logger = logger
# If no file specified, attempt to look for file in
current directory.
# If not found in current directory, look for file in
Gem directory.
unless file
file = if File.exist?(CONFIG_FILE)
CONFIG_FILE
else
file = File.expand_path(File.join(File.dirname
(__FILE__), '..'))
File.basename(file) == 'lib' ? File.join(file,
'..', CONFIG_FILE) : File.join(file, CONFIG_FILE)
end
end
@file = File.expand_path(file)
@config = {}
end
On 3 Mar 2008, at 11:21, Dan Webb wrote:
> Hello all,
>
> Having a problem that I can't successfully google and I wondered if
> any of you had come across the problem before and have any wisdom.
> Here goes...
>
> I'm writing a gem where some of the code in the lib dir refers to a
> yaml file in the data directory of the gem. In the code I try to load
> the yaml file like this:
>
> YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data',
> 'country_codes.yml'))
>
> But, when you load the gem it turns out that __FILE__ is set to:
>
> /Library/Ruby/Site/1.8/country_codes.rb
>
> Instead of the actual file. Which is annoying but hey, its all the
> fun of ruby. My question is though, how can I refer the installed gem
> directory?
>
> Cheers,
>
> --
> Dan Webb
> http://www.danwebb.net
>
> aim: danwrong123
> skype: danwrong
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
More information about the Chat
mailing list