From: salamond Date: 2012-06-27T17:12:44+09:00 Subject: how about adding method hook --e89a8f6438ca8212d104c36fc741 Content-Type: text/plain; charset=ISO-8859-1 Hi, guys. I want An agent on machine, so that it can fake user actions. class Agent def initialize(user, dir) @user = user @dir = dir end def mkdir(dir) bak = Process.euid `mkdir` Process.euid = bak end def create_file(file, content) bak = Process.euid `echo #{content} > #{file}` Process.euid = bak end end My problem is, I have a very long list of actions, like mv, touch, mkdir, rm, etc. I don't want to add save and restore euid for all methods. I kind of guess here I should use some meta programming skills. here's what i tried. class Agent def self.as_me_do(method_name, &block) define_method method_name do block end end as_me_do mkdir do `mkdir #{dir}` end end but this still generate code duplications, and it doesn't work. I'm not sure how this could be done gracefully. Any one been here before? thanks Yan Jiang --e89a8f6438ca8212d104c36fc741 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, guys.

I want An agent on machine, so that it can fak= e user actions.

class Agent
=A0 def init= ialize(user, dir)
=A0 =A0 @user =3D user
=A0 =A0 @dir = =3D dir
=A0 end
=A0 def mkdir(dir)
=A0 =A0 bak =3D Process= .euid
=A0 =A0 `mkdir`
=A0 =A0 Process.euid =3D bak
=A0 end
=A0 def create_file(file, content)
=A0 = =A0 bak =3D Process.euid
=A0 =A0 `echo #{content} > #{file}`
=A0 =A0 Process.euid = =3D bak
=A0 end
end

My problem= is, I have a very long list of actions, like mv, touch, mkdir, rm, etc.
I don't want to add save and restore euid for all methods.
I kind of guess here I should use some meta programming skills= . here's what i tried.

class Agent
=A0 def self.as_me_do(method_name, &block)
=A0 =A0 =A0define_= method method_name do
=A0 =A0 =A0 =A0 block
=A0 =A0 =A0= end
=A0 =A0end
=A0=A0
=A0 as_me_do mkdir do
=A0 =A0 `mkdir #{dir}`
=A0 end
end

but this still generate= code duplications, and it doesn't work.
I'm not sure how= this could be done gracefully.

Any one been here = before?

thanks

Yan Jiang
--e89a8f6438ca8212d104c36fc741--