Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created April 23, 2010 01:37
Show Gist options
  • Select an option

  • Save xaviershay/376059 to your computer and use it in GitHub Desktop.

Select an option

Save xaviershay/376059 to your computer and use it in GitHub Desktop.
# Pathname/File require alternative?
require File.instance_eval {
expand_path(join(dirname(__FILE__), '..', 'bin', path))
}
@matthewtodd
Copy link

I like the way this code looks -- the core idea of sending messages to self is nice.

File.expand_path takes an optional second argument, so you could:

require File.instance_eval {
  expand_path(join('..', 'bin', path), dirname(__FILE__))
}

or even

require File.instance_eval {
  expand_path(join('..', '..', 'bin', path), __FILE__)
}

I had thought 1.9 was introducing a File.relative_path, but I don't see it at http://ruby-doc.org/ruby-1.9/

Barring context, another alternative (that I'm going for these days) is to Lean Hard on the $LOAD_PATH. Let Something Else handle it -- explicitly at development time with Rake, ENV['RUBYLIB'], ruby -I, or implicitly in deployment via the package manager (RubyGems/Bundler, rip, ...).

One trick to make development-time less cumbersome is to fall back to a relative path with a warning if the file can't be found on the $LOAD_PATH. I first saw this in ronn: http://github.com/rtomayko/ronn/blob/0.5/bin/ronn#L100-111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment