From: Logan Capaldo Date: 2006-05-11T00:10:14+09:00 Subject: Re: where do you require? On May 10, 2006, at 10:49 AM, 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? > require only loads a given file once regardless of how many times it's called. I believe _why is doing the require's in methods so that a) it only goes to the disk to require something if it's _really_ needed and b) He's putting the require as close as possible to the place where it's actually used so that when reading the code you don't see a bunch of requires at the top and wonder where in the code they are used. Regardless the require still acts globally. > 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 > That's exactly what it is. OTOH it's only more typing if you're only creating one or two methods, e.g.: class ParkPlace class << self def foo end def bar end def baz end end v.s. class ParkPlace def self.foo end def self.bar end def self.baz end end > Thanks, > Jeff > >