Is require File.expand_path(..., __FILE__)
the best way to require other files within a project?
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.
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.
__dir__ is a alias to a function As the ruby documentation says __dir__ is equal to File.dirname(File.realpath(__FILE__))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With