Skip to content

Instantly share code, notes, and snippets.

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