Created
November 2, 2010 12:47
-
-
Save banister/659561 to your computer and use it in GitHub Desktop.
exploiting the fact that a method aliasing itself adds the method onto the m_tbl of the receiver
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
# Traits in Ruby: | |
module M | |
def self.included(c) | |
c.class_eval do | |
instance_methods.each do |m| | |
alias_method m, m | |
end | |
end | |
c.uninclude(self) | |
end | |
def hello | |
:hello | |
end | |
end | |
class C | |
include M | |
end | |
# hello method injected directly into m_tbl of C | |
C.ancestors #=> [C, Object, ...] # Module not included | |
C.new.hello #=> hello # Yet method still accessible | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment