Created
September 5, 2015 18:50
-
-
Save CUXIDUMDUM/7a4243ac8e3a2d76d514 to your computer and use it in GitHub Desktop.
ruby logging both class and instance level
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
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