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

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to