Search code examples
rubyrequire

ruby require_relative gives LoadError: cannot infer basepath inside IRB


I am currently in

Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/units/

I can go into irb and require a file but it's a really long require...

require '/home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/units/login_as_admin_spec.rb'
=> true

I want to use require_relative, as in

$ cd /home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/
$ pwd
/home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day
$ irb
irb(main):001:0> require_relative 'units/login_as_admin_spec.rb' 

but I get:

LoadError: cannot infer basepath

Solution

  • require_relative requires a file relative to the file the call to require_relative is in. Your call to require_relative isn't in any file, it's in the interactive interpreter, therefore it doesn't work.

    You can use the long form of require by explicitly passing the full path:

    require './units/login_as_admin_spec.rb'
    

    Or you add the current directory to the $LOAD_PATH and just require as usual:

    $LOAD_PATH << '.'
    require 'units/login_as_admin_spec'