Created
June 27, 2012 09:18
-
-
Save rklemme/3002732 to your computer and use it in GitHub Desktop.
Wrap all methods with a specific code
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 Foo | |
def self.method_added(m) | |
t = Thread.current | |
unless t[:_hook] | |
t[:_hook] = true | |
begin | |
alias_method "_#{m}", m | |
class_eval %Q{def #{m}(*a, &b) | |
bak = Process.euid | |
begin | |
_#{m}(*a, &b) | |
ensure | |
Process.euid = bak | |
end | |
end} | |
ensure | |
t[:_hook] = false | |
end | |
end | |
end | |
def x(a) | |
puts a | |
end | |
end | |
Foo.new.x 1 |
oh. i'm jarodzz in ruby-talk.
thanks again for this nice solution.
got it.
the (*a, &b) is for block args.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2 questions that I don't understand.
bak...
begin
_#{m} (_a)
ensure
end
end
do we really need &b?