Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Created June 21, 2013 15:28
Show Gist options
  • Save fuzzy/5831991 to your computer and use it in GitHub Desktop.
Save fuzzy/5831991 to your computer and use it in GitHub Desktop.
an example of metaprogramming in ruby
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