Friday, May 3, 2019

Ruby - How to make your program run from any directory (with libraries)



#!/usr/bin/ruby

### Save the folder name that this lives in
folder = File.expand_path('.',__dir__)

### Add to the Ruby default load path the pwd this was executed from
$LOAD_PATH.unshift(folder) unless $LOAD_PATH.include?(folder)

### Here we can now interpolate a relative path
load "#{folder}/../lib/modules.rb"

puts "stub.rb pwd: #{Dir.pwd}"

No comments:

Post a Comment