From: Sergey Sheypak Date: 2010-02-11T18:32:45+09:00 Subject: Re: Newbee question. Runtime classloading Xavier Noria wrote: > Ruby needs to interpret the file where the class is defined before you > can use it. I understand it. >Interpreting the file has the side-effect of defining the > constant, which is in turn a side-effect of the class keyword. don't get idea with side-effects > As you surely know that is typically done with require, and if the > parsers are unknown beforehand you can just use Dir[] with a glob to > require whatever has some filename pattern in a loop or something. I > mean, require is a method you can call it wherever you want. Something like: require "parsers/parser_format#{@format_version.gsub(/\./, '_')}" clz = "ParserFormat#{@format_version.gsub(/\./, '_')}" parser_impl = Kernel.const_get(clz) works nicely! > Rails does not need that because it offers class/module autoloading: > if the filename is named after the class using some common sense > conventions, and lives in some of the standard directories, > app/something, lib, etc., then the dependencies mechanism goes and > loads it for you the first time the constant is used. That is > accomplished basically via a Ruby hook called const_missing that is > triggered when that initial failure happens. Thanks! -- Posted via http://www.ruby-forum.com/.