module X
def self.included(mod)
define_method ... # define method from here
end
end
module N
# <- define method to this namespace (N)
module M
include X
end
end
T.
module X
def self.included(mod)
define_method ... # define method from here
end
end
module N
# <- define method to this namespace (N)
module M
include X
end
end
T.
one way:
harp:~ > cat a.rb
module X
def self::included other
class << other
eval <<-code
def foo
42
end
code
end
super
end
end
module N
module M
include X
end
end
p N::foo
harp:~ > ruby a.rb
42
nicer way:
harp:~ > cat a.rb
module X
module ClassMethods
def foo
42
end
end
module InstanceMethods
def foo
'forty-two'
end
end
def self::included other
other.module_eval{
extend ClassMethods
include InstanceMethods
}
end
end
class N
class M
include X
end
end
p N::foo
nm = N::new
p nm.foo
harp:~ > ruby a.rb
42
"forty-two"
kind regards.
-a
On Thu, 15 Sep 2005, Trans wrote:
module X
def self.included(mod)
define_method ... # define method from here
end
endmodule N
# <- define method to this namespace (N)
module M
include X
end
endT.
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
batsman@tux-chan:~/src/ruby/ruby.head$ cat /tmp/fgdsdgdfg.rb
module X
def self.included(mod)
# for top-level module, parent = self; change for other semantics
parent = Object.const_get(mod.to_s.sub(/::[^:]+\z/, ""))
parent.send(:define_method, :foo){ puts "#{parent.to_s}#foo" } #fcall in 1.9
class << parent; self end.send(:define_method, :bar) do #fcall in 1.9
puts "#{parent.to_s}.bar"
end
end
end
module M
module N
include X
end
end
Class.new{ include M }.new.foo
M.bar
batsman@tux-chan:~/src/ruby/ruby.head$ ruby /tmp/fgdsdgdfg.rb
M#foo
M.bar
On Thu, Sep 15, 2005 at 01:36:34PM +0900, Trans wrote:
module X
def self.included(mod)
define_method ... # define method from here
end
endmodule N
# <- define method to this namespace (N)
module M
include X
end
end
--
Mauricio Fernandez
Ara, I guess I did not explain well enough. Instead of
p N::foo
I need
p N::foo
T.
Mauricio,
That did the trick! Me thinks I will be generalizing the whole
parent = Object.const_get(mod.to_s.sub(/::[^:]+\z/, ""))
thing in Nano.
Thanks,
T.
Oh,
#fcall in 1.9
I really hope not --an odd named meta-programming method. Please, I
beg, use #instance_send.
T.
parent = Object.const_get(mod.to_s.sub(/::[^:]+\z/, ""))
parent = Object.const_get(mod.name[/\w+/])
... should suit. [?]
class Module
def nesting
n = []
name.split(/::/).inject(self){ |mod, name| c = mod.const_get(name)
; n << c ; c }
return n
end
end
I wrote:
> parent = Object.const_get(mod.to_s.sub(/::[^:]+\z/, ""))
parent = Object.const_get(mod.name[/\w+/])
... should suit. [?]
Cuh! As soon as I hit the button -- my nonsense, sorry.
daz