Module#include (no need for ClassMethods)

T

Trans

If we define Module#include as:

class Module

alias _include include

def include(*mods)
mods.each do |mod|
if mod.const_defined?:)Self)
extend mod::Self
# pass it along if module
if instance_of?(Module)
const_set:)Self, Module.new) unless const_defined?:)Self)
const_get:)Self).send:)include, mod::Self)
end
end
end
_include(*mods)
end

end

Example:

module X
def x; "x"; end

module Self
def q; "q"; end
end
end

class C
include X
end

C.q #=> "q"

Benefits:

* Documents well in RDoc
* Fairly concise and clear
* Self module fits in well with "class << self"
* No callbacks used.

T.
 
M

MonkeeSage

If we define Module#include as:

class Module

alias _include include

def include(*mods)
mods.each do |mod|
if mod.const_defined?:)Self)
extend mod::Self
# pass it along if module
if instance_of?(Module)
const_set:)Self, Module.new) unless const_defined?:)Self)
const_get:)Self).send:)include, mod::Self)
end
end
end
_include(*mods)
end

end

Example:

module X
def x; "x"; end

module Self
def q; "q"; end
end
end

class C
include X
end

C.q #=> "q"

Benefits:

* Documents well in RDoc
* Fairly concise and clear
* Self module fits in well with "class << self"
* No callbacks used.

T.

Clever. :) It doesn't seem to grok class vars inside of ::Self (?),
but class/module_eval works. Weird since class methods work without
the eval.

Regards,
Jordan
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top