0

I have a gem that I have written that has a number of handlers, each of which has their own ruby file in the gem. I need to add the ability to specify a file on the command line that will be loaded in the same manner as these other handlers. The file will typically not be in the default load path of the gem.

I'm not sure the best way to do this. I could take the filename, and then add the containing directory to the load path and then load the file. I could have the user specify a directory containing handlers to be read instead of specifying the file, or I'm sure there are a better way to do it that I haven't yet thought of.

Jericon
  • 4,992
  • 3
  • 20
  • 22
  • Can you provide an example file tree? – Chris Heald Apr 11 '13 at 01:31
  • Why not just specify the full path load("/path/to/some/directory"). this will start at the very root of the drive ruby is installed on. I use this all the time to load custom scripts in irb. – engineersmnky Apr 11 '13 at 18:25
  • @ChrisHeald: The directory tree is not relevant here. The files within the gem are standard format, but the issue is loading a file from outside of the gem, which can be anywhere the user specifies. – Jericon Apr 11 '13 at 20:01
  • @engineersmnky I have tried this in the past, but get errors that the file does not exist, likely because it is not in the load path. – Jericon Apr 11 '13 at 20:02
  • have your tried require_relative? You can then supply a relative path from the gem to the file – engineersmnky Apr 12 '13 at 14:05
  • That actually did the trick, to a degree. I'll post the fix as an answer. – Jericon Apr 17 '13 at 05:45

1 Answers1

0

This was fixed using require_relative and expanding the file path using Dir.pwd:

req_path = File.expand_path(arg, Dir.pwd)
require_relative req_path
Jericon
  • 4,992
  • 3
  • 20
  • 22