Skip to content

@rklemme /method-wrap.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Wrap all methods with a specific code
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
@jarodzz

2 questions that I don't understand.

  1. what is thread for?

  2. def #{m} (a)
    bak...
    begin
    _#{m} (
    a)
    ensure
    end
    end

do we really need &b?

@jarodzz

oh. i'm jarodzz in ruby-talk.

thanks again for this nice solution.

@jarodzz

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
Something went wrong with that request. Please try again.