Created
June 21, 2013 15:28
-
-
Save fuzzy/5831991 to your computer and use it in GitHub Desktop.
an example of metaprogramming in ruby
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
class Logging | |
def initialize | |
@PROMPTS = { | |
:info => '>>'.bold.green, | |
:warn => '**'.bold.yellow, | |
:error => '!!'.red, | |
:fatal => '!!'.bold.red, | |
:debug => '**'.bold.cyan | |
} | |
@LOG = File.open('/tmp/builder.log', 'a+') | |
end | |
def method_missing(method, *args, &block) | |
if method =~ /^(info|warn|error|fatal|debug)/ | |
out = "#{@PROMPTS[method.to_sym]} #{args.join ' '}" | |
@LOG.write out | |
puts out | |
else | |
super.method_missing(method, *args, &block) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment