Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty version of Ruby's require statement?

I've always thought this sort of thing ugly:

require File.join(File.dirname(__FILE__), 'hirb/config')

Is there a prettier alternative, maybe one written for Rails?

require_relative 'hirb/config'
require_relative '../another/file'
like image 573
Mario Avatar asked Dec 04 '22 14:12

Mario


2 Answers

The best approach is probably preparing your load path so you don't need to do all this. It's not especially difficult for your main module or init file to introduce a few other locations.

This is also affected by the RUBYLIB environment variable, as well as the -I command line parameter.

$: << File.expand_path(File.join('..', 'lib'), File.dirname(__FILE__))
like image 61
tadman Avatar answered Jan 02 '23 23:01

tadman


You could do

Dir.chdir(File.dirname(__FILE__) do
  require 'hirb/config'
  require '../another/file'
end

Whether or not that's better is a matter of taste, of course.

like image 24
sepp2k Avatar answered Jan 03 '23 00:01

sepp2k