From: ts Date: 2005-11-26T01:28:28+09:00 Subject: Re: Dynamic finding of missing constants. >>>>> "O" == Ola Bini writes: O> I'm doing some slightly interesting things with Ruby, and I wanted to know O> of there's a language way of interrupting the process for finding a O> Constant name, like the method_missing method. You have Module#const_missing moulon% ri Module#const_missing --------------------------------------------------- Module#const_missing mod.const_missing(sym) => obj ------------------------------------------------------------------------ Invoked when a reference is made to an undefined constant in _mod_. It is passed a symbol for the undefined constant, and returns a value to be used for that constant. The following code is a (very bad) example: if reference is made to an undefined constant, it attempts to load a file whose name is the lowercase version of the constant (thus class +Fred+ is assumed to be in file +fred.rb+). If found, it returns the value of the loaded class. It therefore implements a perverse kind of autoload facility. def Object.const_missing(name) @looked_for ||= {} str_name = name.to_s raise "Class not found: #{name}" if @looked_for[str_name] @looked_for[str_name] = 1 file = str_name.downcase require file klass = const_get(name) return klass if klass raise "Class not found: #{name}" end moulon% Guy Decoux