----- Original Message -----
From: Albert Wagner <alwagner / tcac.net>
To: ruby-talk ML <ruby-talk / ruby-lang.org>
Sent: Wednesday, May 30, 2001 7:14 PM
Subject: [ruby-talk:16068] require and include confusion


> I have totally confused myself about the relationship between require and
> include. What is included must be already loaded by require.  So why
include
> at all?  How do you know what needs to be included?

An interesting question... the simple answer is that require and include are
essentially unrelated.

That is: 1. You can do a require and then not need an include. 2. You can
define a module in the current file, so that you can do an include without
needing a require.

"require" is similar to the C include, which may cause newbie confusion.
(One notable difference is that locals inside the required file "evaporate"
when the require is done.)

The Ruby include is nothing like the C include.

The include statement "mixes in" a module into a class. It's a limited form
of multiple inheritance. An included module literally bestows an "is-a"
relationship on the thing including it.


module FlyingCreature
  def Fly
  end
end

class Bird
  include FlyingCreature
  #...
end

class Bat < Mammal
  include FlyingCreature
  #...
end

dracula = Bat.new

Notice that dracula.is_a? Mammal is true, and so is dracula.is_a?
FlyingCreature.

What about an include as you showed it, outside of a class definition? Well,
it
becomes part of Object. (Remember that Object is a class.) So, unless I'm
grossly mistaken, if you "include Wombat" at the top level, every object x
created
from then on would give true for x.is_a? Wombat, whether it's an array, a
string,
a class, or whatever.

Better?

Hal