Why isn't current directory on my Ruby path? [duplicate]
In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about require_relative
. My apps tend to look like this:
require 'some_gem'require 'another_gem'require_relative 'lib/init'
And then lib/init.rb
can have:
require_relative 'lib1' # this is lib/lib1.rbrequire_relative 'lib2' # this is lib/lib2.rb
It's the bees knees, and solves all sorts of problems I used to have with requiring the same file from different working directories.
Edit: Unfortunately (for reasons I don't know and haven't looked into) require_relative
doesn't work specifically in irb. For this you can:
- do what you initially described: either
$: << '.'
or$:.unshift '.'
, or you can use
load 'myfile.rb'
orrequire './myfile'
instead:irb(main):001:0> Dir['*.rb']=> ["a.rb", "bar.rb", "foo.rb", "prime.rb", "tmp.rb"]irb(main):002:0> require 'a'LoadError: no such file to load -- a from <internal:lib/rubygems/custom_require>:29:in `require' from <internal:lib/rubygems/custom_require>:29:in `require' from (irb):2 from /usr/local/bin/irb:12:in `<main>'irb(main):003:0> require_relative 'a'LoadError: cannot infer basepath from (irb):3:in `require_relative' from (irb):3 from /usr/local/bin/irb:12:in `<main>'irb(main):004:0> load 'a.rb'a=> trueirb(main):005:0> require './a'a=> true