From: Mauricio Fernandez Date: 2006-05-11T00:15:06+09:00 Subject: Re: where do you require? On Wed, May 10, 2006 at 11:49:37PM +0900, Jeff Rose wrote: > I was reading through some of Why's parkplace code this afternoon, and I > saw a couple things I wanted to ask people about. > (http://code.whytheluckystiff.net/parkplace/browser/trunk/lib/parkplace.rb) > > 1) What's the effect of doing a require inside a method like in > ParkPlace#config or ParkPlace#serve? It's just a method right, so it > will re-require every time the method is called? Since it's getting > loaded inside a method does it garbage collect the library after the > method returns? (Maybe do it to save memory?) What's the behavior with > bindings that use dynamically linked libraries? It will only get loaded once. (In the first call to #require its name will get added to $", which is checked against before loading a file via require). Placing the call to #require inside the method is but a way to load code lazily. As for the code being GC'd, it will all depend on whether something still holds a reference to the associated NODEs or not. If the constant that refers to a class/module is removed and there are no instances (or UnboundMethod objects, etc...) left, the code will be GCed (as would an old instance method when you redefine it). > 2) What is this type of class definition all about: > > module ParkPlace > class << self > def foo > end > end > end > > > It seems to be creating singleton methods the same as doing this, except > with more typing: > > module ParkPlace > def ParkPlace.foo # or def self.foo > end > end class << self; ... end introduces a new scope. It's useful in cases like class X class << self attr_reader :foo # getter for the class instance variable end ... end -- Mauricio Fernandez - http://eigenclass.org - singular Ruby