I am using g a logger in all of my classes. I want each msg to begin with class name and method name like so:
Class_name::Method_name
this is what i'm doing now :
class FOO
def initialize
end
def bar
msg_prefix = "#{self.class}::#{__method__}"
... some code ...
@logeer = "#{msg_prefix} msg ..."
end
def bar2
msg_prefix = "#{self.class}::#{__method__}"
... some code 2 ...
@logeer = "#{msg_prefix} msg2 ..."
end
end
i want to use a before_filter like in rails to prevent duplicity,
I am using sinatra
but the classes are plain old ruby 1.9.3
classes
ideas??
You can use ActiveModel::Callbacks to get
before_filter
-like behaviour in plain Ruby classes (though perhaps in your case it's overkill for just executing one line):You can get a callback on any method being created with Module#method_added, alias the old method, then define a new method that calls the before_filter method first. Here's my (extremely) rough first concept:
By using
__method__
like you were increate_prefix
, you'll get the name of the filter method, not the original method, so you have to pass the method name in. There might be other solutions to make that a bit cleaner.