Skip to content

Instantly share code, notes, and snippets.

@rtyler

rtyler/wut.rb Secret

Created August 20, 2015 15:46
Show Gist options
  • Save rtyler/ac5476c02447cf34971e to your computer and use it in GitHub Desktop.
Save rtyler/ac5476c02447cf34971e to your computer and use it in GitHub Desktop.
# JRuby require methods differs from MRI when requiring absolute paths. It will require the same
# file multiple times if the path given to require differs, even when the canonical path would be
# the same.
#
# e.g.
#
# $LOAD_PATH = ['/home/me/project']
# require 'foo'
# require '/home/me/project/foo.rb'
#
# The above would require the foo.rb file twice.
#
# This method requires all the file matching a given glob pattern from the root directory.
#
# e.g.
#
# Dir['foo/*.rb'] # => ['foo/a.rb', 'foo/b.rb']
# require_glob('foo/*.rb')
#
# would end up calling
#
# require 'foo/a'
# require 'foo/b'
def self.require_glob(pattern)
pattern = File.join(L4E.root, pattern)
Dir[pattern].sort.each do |f|
to_require = Pathname(f).relative_path_from(Pathname(L4E.root)).to_s
# Trim .rb
to_require = to_require.gsub(/\.\w+$/, "")
require to_require
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment