Created
April 30, 2012 11:36
-
-
Save prodis/2557515 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Módulos para definir métodos globais
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
require 'logger' | |
module CoolLog | |
def logger | |
@cool_log_logger ||= ::Logger.new(STDOUT) | |
end | |
def level | |
@cool_log_level ||= :info | |
end | |
def level=(value) | |
@cool_log_level = value | |
end | |
def log(message) | |
logger.send(level, message) | |
end | |
end |
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 MyClass1 | |
include CoolLog | |
end | |
m1 = MyClass1.new | |
m1.level # => :info | |
m1.log "Informando..." # => I, [2012-04-30T08:25:22.436474 #2601] INFO -- : Informando... | |
m1.level = :debug | |
m1.log "Debugando..." # => D, [2012-04-30T08:25:22.437045 #2601] DEBUG -- : Debugando... |
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 MyClass2 | |
extend CoolLog | |
end | |
MyClass2.level # => :info | |
MyClass2.log "Informando..." # => I, [2012-04-30T08:43:37.252694 #2654] INFO -- : Informando... | |
MyClass2.level = :debug | |
MyClass2.log "Debugando..." # => D, [2012-04-30T08:43:37.252736 #2654] DEBUG -- : Debugando... |
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 MyClass3 | |
extend CoolLog | |
end | |
class MyClass4 | |
extend CoolLog | |
end | |
MyClass3.level # => :info | |
MyClass4.level # => :info | |
MyClass3.level = :debug | |
MyClass3.level # => :debug | |
MyClass4.level # => :info |
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 MyModule | |
extend CoolLog | |
end | |
MyModule.level # => :info | |
MyModule.log "Informando..." # => I, [2012-04-30T09:07:20.251521 #2740] INFO -- : Informando... | |
MyModule.level = :debug | |
MyModule.log "Debugando..." # => D, [2012-04-30T09:07:20.251561 #2740] DEBUG -- : Debugando... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment