Advertisement
Guest User

Untitled

a guest
Jul 17th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.45 KB | None | 0 0
  1.   def run_code(code)
  2.     # run something
  3.     stdout_id = $stdout.to_i
  4.     $stdout = StringIO.new
  5.     cmd = <<-EOF
  6.     $stdout = StringIO.new
  7.     begin
  8.       #{code}
  9.     end
  10.     EOF
  11.     begin
  12.       result = Thread.new { Kernel.eval(cmd, @binding) }.value
  13.     rescue SecurityError
  14.       return "illegal"
  15.     rescue Exception => e
  16.       return e
  17.     ensure
  18.       output = get_stdout
  19.       $stdout = IO.open(stdout_id)
  20.       $stdout.close
  21.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement