This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Running Code as Another User
CodeExpectedActual
module Process
  def as_uid(uid)
    old_euid, old_uid = Process.euid, Process.uid
    Process.euid, Process.uid = uid, uid
    begin
      yield
    ensure
      Process.euid, Process.uid = old_euid, old_uid
    end
  end	
  module_function(:as_uid)
end
Dir.mkdir("as_root")
Process.as_uid(1000) do
  Dir.mkdir("as_leonardr")
  %x{whoami}
end
"leonardr\n" "leonardr\n"
Process.as_uid(1000) do
  Dir.rmdir("as_root")
end
Errno::EPERM: Operation not permitted - as_root
Dir.rmdir("as_root")
0
Error! (Exception?) Here's stdout:
Errno::ENOENT: No such file or directory - as_root
	from (irb):21:in `rmdir'
	from (irb):21
system 'runas /user:frednerk ruby script_two.rb'
false