From: Dave Burt Date: 2006-05-11T00:17:00+09:00 Subject: Re: where do you require? Hi, Jeff, 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 any library once. So, yes, require will be called each time the method is called, but it will only load the library the first time. > 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 Yes, it's the same. It makes more sense if you have a number of class methods -- it becomes a way of grouping them together in your code, and makes the method names ("def" lines) easier on the eye. Cheers, Dave