Rodrigo Alves

Adding require_relative to IRB

Ruby 1.9 brought require_relative to the rubyists' life and it’s a nice thing. If you like it, you probably would like to use it in the interactive console.

For some reason I don’t yet know for sure, require_relative does not work on IRB:

>> require_relative "doom"
LoadError: cannot infer basepath
    from (irb):2:in `require_relative'
   from (irb):2
   from /Users/rodrigovieira/.rbenv/versions/1.9.3-p125/bin/irb:12:in `<main>'

So, I needed this an implemented this method and added it to the .irbrc file, nothing complex, but I found it useful still:

module Kernel
  def require_relative(file)
    $:.unshift Dir.pwd
    require file

Hope it is as helpful to you as it is to me, I’ll update this post whenever I found why this beloved method does not work in the console.

Rodrigo Alves is a 19 year-old hacker from Paulista, Brazil. He studies Computer Science at UFPE. Read more about him.