logo       
Google Custom Search
    AddThis Social Bookmark Button

[ruby-dev:27836] autoload with const_missing: msg#00220

Subject: [ruby-dev:27836] autoload with const_missing
 ささだです。

 autoload を Module#const_missing を利用した処理にしませんか。具体的に
は以下のような処理になります。


$AUTOLOAD = {}

class Module
  def const_missing const
    if $AUTOLOAD.has_key? const
      require $AUTOLOAD.fetch(const)
      self.const_get(const)
    else
      begin
        raise NameError.new("uninitialized constant #{const}")
      rescue NameError => e
        e.backtrace.shift
        raise e
      end
    end
  end
end

def autoload sym, file
  $AUTOLOAD[sym.to_sym] = file
end

# 例
autoload(:CGI, 'cgi')

p CGI
p Hoge


利点:
・autoload の処理を評価器にべったり書かないので綺麗
・$AUTOLOAD というグローバル変数を導入するので、$LOADED_FEATURES みたい
に autoload の設定が扱いやすくなる


 どうでしょうか。
-- 
// SASADA Koichi at atdot dot net






Try Searching:
servers, voip, java, networking, microsoft ...
<Prev in Thread] Current Thread [Next in Thread>