From: shevegen@... Date: 2018-07-20T19:17:48+00:00 Subject: [ruby-core:88031] [Ruby trunk Feature#14927] Loading multiple files at once Issue #14927 has been updated by shevegen (Robert A. Heiler). I wanted to propose a more sophisticated load-process in ruby some time ago, but I never got around it. I am even thinking of being able to load files based on abbreviations/shortcuts, without necessiting a hardcoded path (e. g. require needs the path, whereas with an abbreviation we could only refer to that abbreviation, and an internal list keeps track of where the actual file resides instead). But it's not so simple to suggest something that has a real chance of inclusion. I am glad to see other people have somewhat similar ideas - of course your suggestion is quite different from my idea, but you tap into a very similar situation: - Handling multiple files. This is especially useful for larger ruby projects. For smaller projects it is not so important perhaps but when you have like +50 .rb files and growing, making things easier in regards to handling files, would be great. To the suggestion - I think several ruby hackers may benefit from better handling of files. I am not sure if there is a big chance to see load() and require() itself being changed, but I also don't know. I think we should ask matz, but there may be a chance that they may not be changed, possibly due to backwards compatibility (if there is a problem). In the long run we may want to consider using alternative means. For example, the require-family, such as require_relative(). I don't mean require_relative in itself, but something related to require_*. require_relative also handles location to other files, just relative to the directory at hand. By the way, I also understand this use case: > This approach may not work if your files have dependencies like that: And it is related to another use case which isn't a lot of fun: - Circular dependencies + warnings about this I also thought about this with my never-written proposal... :D Circular dependency warnings are not a lot of fun IMO. I think Hiroshi Shibata also had a suggestion in regards to ... require, I think, some months or a few years ago, but I don't remember what it was exactly. Anyway, before I write way too much and digress from the suggestion, I am in general in favour of your suggestion. I don't have any particular opinion on your proposed solution - another API may be fine or perhaps a new method... load_files() ? Hmm... may not be an ideal name either. But I think the specific API may be a detail. The more important aspect is whether ruby can provide easier means for ruby users to load or require a batch of files. Perhaps load() and require() will remain as they are, for simplicity and backwards compatibility, but in such a case we could think about better ways to handle the task of "pulling all necessary files" into a project. This may also help people when they create gems. In my own larger gems I do something very similar as to what S��bastien Durand showed, e. g. I also do Dir['*.rb'] often on a per-directory basis. That way I don't have to specify the names of the individual .rb files. ---------------------------------------- Feature #14927: Loading multiple files at once https://bugs.ruby-lang.org/issues/14927#change-73048 * Author: deneb (S��bastien Durand) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Just a proof concept I wanted to share. Maybe it could be useful? Say you want to load all the .rb files in your lib directory: ~~~ ruby Dir['lib/**/*.rb'].each { |file| load(file) } ~~~ This approach may not work if your files have dependencies like that: ~~~ ruby # lib/foo.rb class Foo < Bar end ~~~ ~~~ ruby # lib/bar.rb class Bar end ~~~ Foo class needs Bar class. You will get a NameError (uninitialized constant Bar). So in my personal projects, I use this algorithm to load all my files and to automatically take care of dependencies (class/include): ~~~ ruby def boot(files) i = 0 while i < files.length begin load(files[i]) rescue NameError i += 1 else while i > 0 files.push(files.shift) i -= 1 end files.shift end end end ~~~ ~~~ ruby boot Dir['lib/**/*.rb'] # It works! foo.rb and bar.rb are properly loaded. ~~~ My point is: it would be cool if Kernel#load could receive an array of filenames (to load all these files in the proper order). So we could load all our libs with just a single line: ~~~ ruby load Dir['{path1,path2}/**/*.rb'] ~~~ -- https://bugs.ruby-lang.org/ Unsubscribe: