Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the quickest way to recursively add directories onto the $LOAD_PATH in ruby?

Tags:

ruby

I'd like to slap everything in vendor/gems/gems/**/lib onto the load path. I've got a way to do it, but it just feels clunky. Right now, I'm doing it via:

base = File.expand_path(File.join(File.dirname(__FILE__), "..", "vendor", "gems", "gems"))
libs = File.join(base, "**", "lib")
Dir.glob(libs) { |lib| $LOAD_PATH.unshift lib}

I'm sure there's a better way to do this.

like image 983
bergyman Avatar asked Oct 23 '25 18:10

bergyman


1 Answers

i think setting up the base path via File is fine. but you don't need to iterate over the list of directories to put them into the $LOAD_PATH. you could use unshift and expand the array.

libs = File.expand_path("../../vendor/gems/**/lib", __FILE__)
$LOAD_PATH.unshift *Dir.glob(libs)
like image 97
rubiii Avatar answered Oct 26 '25 09:10

rubiii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!