Title / Description
Code require 'rubygems' require 'terminator' class Class def unsafe_methods(*methods) methods.each do |method| self.class_eval "def safe_and_#{method}; sandbox('#{method}'); end;" end end end class JustAClass def initialize() end unsafe_methods :slow, :normal def slow() result = '' Terminator.terminate 1 do sleep 2 result = "I'll never print" end result end def normal() result = '' Terminator.terminate 3 do sleep 2 result = "I will print" end result end def method123() "well tested with local method calls which never really causes a problem" end def sandbox(statement) msg = `ruby -e "require 'test-terminator'; jac = JustAClass.new; puts jac.#{statement}"` msg end end if __FILE__ == $0 then jac = JustAClass.new #jac.slow() # <- 1) this code would raise a terminator error # and then terminate the script #puts jac.normal() # <- 2) this code runs fine up until the # the 3 seconds has expired at which point # the script is terminated with the # message 'Terminated' #puts jac.safe_and_slow() # <- 3) executes method slow() and # continues exectution puts jac.safe_and_normal() # <- 4) executes method normal() and # continues exectution puts jac.method123() # <- a regular method which is relatively safe puts 'now sleeping for 3 seconds' sleep 3 puts 'normal code execution can continue' endrequire 'rubygems' require 'terminator' class Class def unsafe_methods(*methods) methods.each do |method| self.class_eval "def safe_and_#{method}; sandbox('#{method}'); end;" end end end class JustAClass def initialize() end unsafe_methods :slow, :normal def slow() result = '' Terminator.terminate 1 do sleep 2 result = "I'll never print" end result end def normal() result = '' Terminator.terminate 3 do sleep 2 result = "I will print" end result end def method123() "well tested with local method calls which never really causes a problem" end def sandbox(statement) msg = `ruby -e "require 'test-terminator'; jac = JustAClass.new; puts jac.#{statement}"` msg end end if __FILE__ == $0 then jac = JustAClass.new #jac.slow() # <- 1) this code would raise a terminator error # and then terminate the script #puts jac.normal() # <- 2) this code runs fine up until the # the 3 seconds has expired at which point # the script is terminated with the # message 'Terminated' #puts jac.safe_and_slow() # <- 3) executes method slow() and # continues exectution puts jac.safe_and_normal() # <- 4) executes method normal() and # continues exectution puts jac.method123() # <- a regular method which is relatively safe puts 'now sleeping for 3 seconds' sleep 3 puts 'normal code execution can continue' endrequire 'rubygems' require 'terminator' class Class def unsafe_methods(*methods) methods.each do |method| self.class_eval "def safe_and_#{method}; sandbox('#{method}'); end;" end end end class JustAClass def initialize() end unsafe_methods :slow, :normal def slow() result = '' Terminator.terminate 1 do sleep 2 result = "I'll never print" end result end def normal() result = '' Terminator.terminate 3 do sleep 2 result = "I will print" end result end def method123() "well tested with local method calls which never really causes a problem" end def sandbox(statement) msg = `ruby -e "require 'test-terminator'; jac = JustAClass.new; puts jac.#{statement}"` msg end end if __FILE__ == $0 then jac = JustAClass.new #jac.slow() # <- 1) this code would raise a terminator error # and then terminate the script #puts jac.normal() # <- 2) this code runs fine up until the # the 3 seconds has expired at which point # the script is terminated with the # message 'Terminated' #puts jac.safe_and_slow() # <- 3) executes method slow() and # continues exectution puts jac.safe_and_normal() # <- 4) executes method normal() and # continues exectution puts jac.method123() # <- a regular method which is relatively safe puts 'now sleeping for 3 seconds' sleep 3 puts 'normal code execution can continue' end
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code