0rromw
Last Updated: February 25, 2016
·
517
· shell
Avatar3

How to write 'require' without explicit path

Ever wonder why you need to write something like this to require file?

require './lib/test-new-feature' 

Even thought there is no '.rb' in the end, it is pretty explicit path.
To make it shorter and more 'native' use following string:

$: << File.expand_path(File.dirname(__FILE__) + “/../lib”)

Now you can write only:

require 'test-new-feature'

Even though it is short path, it have some downsides to it:

  1. Need to track directories automatically
  2. Polluting require path is slowing down require time
Say Thanks
Respond

2 Responses
Add your response

4792
Fc5f974ef4fd49e1bb6a48c669b496ef

Doing this is a Bad Idea. You want, instead, to use require_realtive: http://extensions.rubyforge.org/rdoc/classes/Kernel.html

over 1 year ago ·
4799
Avatar3

Thanks! That is option as well. It is good to know how require_path works as well

over 1 year ago ·