From: Ken Bloom Date: 2009-03-16T01:07:41+09:00 Subject: Re: Basics of Require On Sun, 15 Mar 2009 09:10:39 -0500, Mike Stephens wrote: > I don't have access to an instance of Ruby at the moment and I am not > clear what is being said in The Ruby Programming Language (Flanagan & > Matsumoto). It says require code is executed immediately. It also says > you can't insert the require code in the middle of something as if you > are copy-and-pasting, so can you explain some basics of what actually > happens? > What are the differences between wrapping require code in 'Module', > 'Def' and 'Class'? def method require 'foo' end only loads 'foo' when you call the method. The contents of 'foo' are visible globally. (Unlike a #include in C) module Foo require 'foo' end class Foo require 'foo' end These are exactly the same. They both load 'foo', and the contents of 'foo' are NOT wrapped in the module or class Foo. > The final question is in typical web environments like webrick, apache > or eruby, does the require code get loaded and 'compiled' everytime any > program refers to it or is there some mechanism to cache common code in > 'compiled' form? That depends on the environment. Webrick, which is a web server written in Ruby, keeps the same ruby interpreter running the whole time your server is running, so if you 'require' once, then the code stays loaded the whole time the server is running. With apache, it would depend on how you've set up apache to run the ruby interpreter. Eruby is a library that runs within a ruby interpreter, so it's not really relevant to this question. -- Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/