Created
January 14, 2019 00:22
-
-
Save Mirv/c9b8054a8fbbae20ecac44a9bc49cfb2 to your computer and use it in GitHub Desktop.
Uses require to load a sub-directory by adding sub to $LOAD_PATH
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
## Require (class) | |
# | |
# In: directory to require | |
# Out: | |
# Side: requires all the ruby files found | |
# | |
# Notes: Call via 'load_dir' or 'load_cur_dir' | |
# | |
module DirectoryLoader | |
class Require | |
def self.load_dir(directory) | |
files = Dir.chdir('greet') do | |
Dir["*.rb"] | |
end | |
new(files).execute_dir(directory) | |
end | |
def self.load_cur_dir(directory) | |
files = Dir["*.rb"] | |
new(files).execute_dir(directory) | |
end | |
def initialize(files) | |
@files = files | |
puts "init #{@files}" | |
end | |
def execute_dir(directory) | |
add_to_path(directory) | |
drop_extension | |
dir_load | |
puts Range.new(1,24).in_range?(3) | |
end | |
def drop_extension | |
@files.map!{|x| x = x.split('.').first} | |
end | |
def add_to_path(directory) | |
$: << "#{Dir.pwd}/#{directory}" | |
end | |
def dir_load | |
@files.each{|f| require f} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment