Different types of include

  • Thread starter Daniel Sheppard
  • Start date
D

Daniel Sheppard

How is an 'include' outside of a module different from one inside a
module. Where does the outer 'include' come from (the only documented
include method is on Module).

The following code works:

require 'fox'
include Fox
module MyModule
class MyClass
def hello; puts FXTreeList; end
end
end

The following does not:
module MyModule
require 'fox'
include Fox
class MyClass
def hello; puts FXTreeList; end
end
end

I was under the impression that part of the mechanism of include was to
include the constants defined in the other module.

Is the only way to keep your namespace clean to use Fox::FXTreeList
everywhere and avoid top-level includes?

(this isn't a problem with fox, due to the fact that fox always has FX
on the classname prefix, but I can see that it could be a problem with
other modules).

Daniel Sheppard
http://jroller.net/page/soxbox
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################
 
P

Peter

How is an 'include' outside of a module different from one inside a
module. Where does the outer 'include' come from (the only documented
include method is on Module).

The following code works:

require 'fox'
include Fox
module MyModule
class MyClass
def hello; puts FXTreeList; end
end
end

The following does not:
module MyModule
require 'fox'
include Fox
class MyClass
def hello; puts FXTreeList; end
end
end

I was under the impression that part of the mechanism of include was to
include the constants defined in the other module.

Yes. The first example includes Fox in Object, hence making its constants
visible to all objects. The second example includes it in MyModule, not in
MyClass, so its constants become directly visible to MyModule, not to
MyClass. So the following does work:

module MyModule
require 'fox'
class MyClass
include Fox
def hello; puts FXTreeList; end
end
end

Or this does, but then instead of Fox::FXTreeList, you'd need to type the
longer MyModule::FXTreeList :

module MyModule
require 'fox'
include Fox
class MyClass
def hello; puts MyModule::FXTreeList; end
end
end


Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top