From: John Wilger Date: 2006-12-18T02:52:01+09:00 Subject: Re: require, load and include --- what's the difference? On Dec 17, 9:19 am, matt wrote: > Require -- seems to include another file to gain access to other > classes, modules, and global data. This seems to be equivalent to a C++ > include, with inclusion guards > > Load -- seems to be the same as require, but will actually reload the > data every time it is called, where require will load once and cache the > file. This seems to be equivalent to C++ include, with out inclusion > guards. Basically correct on these two. > Include -- seems to include a module name from a pre-determined path, > but not classes (unless encapsulated within a module). Would this be > similar to a C++ namespace being given the include directory at compile > time? > what's > the advantage of include vs require? I mean, why would I include a > module, rather than require it's filename? Include doesn't really have anything to do with files or paths. For instance, the following code could all be in the same file: ## BEGIN ## Woden:~ jwilger$ irb >> module Foo >> def hello >> "defined in module Foo" >> end >> end => nil >> ?> class Bar >> include Foo >> end => Bar >> ?> x = Bar.new => # >> x.hello => "defined in module Foo" ## END ## When used this way, modules and include are Ruby's preferred way to implement that which would be achieved via multiple inheritance in some other languages.