From: David Masover Date: 2009-11-25T03:06:11+09:00 Subject: Re: [ANN] autoloader, version 0.0.2 On Tuesday 24 November 2009 06:43:20 am Robert Klemme wrote: > Looks fine to me. But I do wonder how you generate the symbol names > for autoload files? Basically autoload needs a symbol name and a file > name. While the file names can be extracted from a directory how do > you find the symbol name? More or less the same way Rails does, which is why I support these various inflection libraries. Basically, I take the file name and munge it so it looks like a constant name. > Maybe users should be able to customize > that algorithm in order to be able to autoload class "FooBar" from > file "foobar.rb" or "foo_bar.rb". Right now, it's specified to be foo_bar.rb. It uses String#to_const_string from extlib, or String#camelize from ActiveSupport. If neither of these are loaded, it tries to load extlib, then activesupport/inflector. Maybe users should be able to customize it. The idea is, of course, convention over configuration. If there's only a single exception (FooBar, for example), you could always manually autoload that one file. > I would at least add a description > how you generate the symbol name from the file name. Neither of these are bad ideas. If you can think of a way to customize the behavior without overcomplicating things, go for it. I'm not motivated, so patches welcome. > Btw, one disadvantage of using a directory glob is that it is far more > expensive than the standard autoload because the standard autoload > does not need any file system IO before any file is loaded. Using > directory globbing on the other hand requires to at least read the > directory which may slow down startup of a script considerably. Except that standard autoload is more work for me, and nearly defeats the point. Trading requires for autoloads directly makes things harder to debug without making them easier to write -- it _only_ optimizes start time. One possibility would be to write a script which attempts to automatically generate these autoload statements, but store them statically in a single file. The main reason I don't like that is, you've now added a "compile" step to a script. It might work as an option, maybe a "development" vs "production" mode. Another possibility is to override const_missing, but that gets complex fast, as Rails shows. Also, keep in mind that the directory IO is shallow. That is, if you tell it to autoload foo, it won't touch anything in foo/bar. If you create a file bar.rb which requires autoload, it will look at foo/bar, but only once Bar is autoloaded. Thanks for the feedback!