Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is require File.expand_path(..., __FILE__) the best practice?

Tags:

ruby

require

Is require File.expand_path(..., __FILE__) the best way to require other files within a project?

like image 280
dan Avatar asked Dec 01 '10 15:12

dan


People also ask

What does __ FILE __ mean in Ruby?

The value of __FILE__ is a relative path that is created and stored (but never updated) when your file is loaded. This means that if you have any calls to Dir.

What does require relative do?

require_relative allows you to "load a file that is relative to the file containing the require_relative statement". With require , ./ indicates a path that is relative to your current working directory.

What is __ DIR __ Ruby?

__dir__ is a alias to a function As the ruby documentation says __dir__ is equal to File.dirname(File.realpath(__FILE__))


1 Answers

In Ruby 1.9.2 + require_relative is probably the more correct way to do it.

require was changed to not include your '.' directory for security reasons. require_relative was added to provide a local-file solution for modules relative to your calling script's path.

You can search here on StackOverflow, particularly in "What is require_relative in Ruby?", and the internets and find usage tricks and the why-for messages explaining how it came about.

like image 116
the Tin Man Avatar answered Sep 19 '22 15:09

the Tin Man