Last active
May 22, 2021 21:10
-
-
Save jofi/5597755 to your computer and use it in GitHub Desktop.
Quick tricks to expand load paths in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# expand_path is genuine and DOES KNOW ABOUT absolute and relative paths | |
File.expand_path('relative/path/to/dir_or_file', '/start/path') # "/start/path/relative/path/to/dir_or_file" | |
File.expand_path('../application', __FILE__) # "/absolute/path/to/FILE/../application" | |
File.expand_path('/path/to/dir_or_file', '/start/path') # "/path/to/dir_or_file" | |
File.expand_path('../path/to/dir_or_file', '/start/path') # "/start/path/to/dir_or_file" | |
# join seems to be STUPID. It just joins strings (and adds file separator if needed) | |
File.join('some/path/', '/other/path.rb') # "some/path/other/path.rb" | |
# modify the global load path: | |
$: #actual load path | |
$:.unshift(File.expand_path('../lib', __FILE__)) | |
$:.unshift('path/to/dir') #insert some directory to loadpath | |
$:.unshift(File.expand_path('test', Rails.root)) # add test directory to the load paths | |
# require (load) a file into the environment | |
require File.expand_path('../../config/environment', __FILE__) # in fact we are using absolute path here | |
require 'relative_path/file' # the whole load path is checked | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment