Created
August 6, 2012 13:35
-
-
Save stevenbristol/3274502 to your computer and use it in GitHub Desktop.
include and extend using included
This file contains hidden or 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
module Log | |
def Log.included cls | |
cls.extend Log #also works by passing self, but Log is less confusing | |
end | |
def log str | |
p str | |
end | |
end | |
class Thing | |
include Log | |
log "works from class" # => "works from class" | |
def test_log | |
log "works from instance" | |
self.log "works from self (in instance)" | |
Thing.log "works from class (in instance)" | |
end | |
end | |
Thing.new.test_log # => "works from instance" | |
# => "works from self (in instance)" | |
# => "works from class (in instance)" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment