Created
August 1, 2009 08:14
-
-
Save dohzya/159603 to your computer and use it in GitHub Desktop.
Example of namespace aware send implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
# invoke the method meth of the module mod | |
def mod_send(mod, meth, *args, &bloc) | |
if self.is_a? mod | |
mod.instance_method(meth).bind(self).call(*args, &bloc) | |
else | |
method_missing("#{mod}::#{meth}", *args, &bloc) | |
end | |
end | |
end | |
if $0 == __FILE__ | |
module M1 | |
def foo | |
puts "#{M1}::#{self}" | |
end | |
end | |
module M2 | |
def foo | |
puts "#{M2}::#{self}" | |
end | |
end | |
class C | |
include M1 | |
include M2 | |
end | |
C.new.mod_send M1, :foo | |
C.new.mod_send M2, :foo | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment