Wednesday, July 19, 2006

Ruby's classes are dynamic

One way cool feature of Ruby is that classes can be modified dynamically. I got a chance to use this feature today when I needed to change the output format of the Logger class in Rails (the default format is suboptimal IMHO). I simply had to append to environment.rb:

require 'logger'
class Logger
def format_message(severity, timestamp, msg, progname)
"#{severity} #{timestamp} #{msg}\n"

and, voila, the method 'format_message' was modified.

As a sidenote: By the time I write this the example for changing the log format given in the Rails wiki does not work. And what's worse, if you put non-working code into your environment.rb WebBrick will, of course, not come up.

No comments: