Nicolas Desprès
12/17/2006 5:47:00 PM
On 12/17/06, matt <matt@kettlewell.net> wrote:
> Hello,
>
Hi,
> I'm browsing through some code, trying to familiarize myself with the
> language (I have a C++ background, with only a #include), and I realize
> that there are 3 calls that I'm trying to wrap my head around.
>
> Here's my stab:
>
> 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.
>
Seems fine to me with the difference that #include is evaluated at
compile time whereas load, require works at run-time (there is no
compile-time in Ruby).
> 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?
>
You should see include as the ruby approach to multiple inheritance.
Include mix in a class the method defined in the given module. This
way you can share methods between different classes.
> So a couple of things here, are my understandings of the 3 methods
> close, if not, could someone please expand on them. The other is what's
> the advantage of include vs require? I mean, why would I include a
> module, rather than require it's filename?
>
require works on a file. It tells ruby to load a file if it is not
already loaded. It is closer to the source shell builtin than the
#include cpp directory from my point of view.
include adds the methods defined in a module into a class.
I hope this can help you.
Cheers,
--
Nicolas Desprès