Class: SafeEval

SafeEval (Class)

In files
lib/safe_eval.rb
Parent
Object

Methods

Public Class methods

This is same as new.safe_eval(code, tm)

これはnew.safe_evalと同じです。(コード、tm)

    # File lib/safe_eval.rb, line 9
 9:         def self.eval(code, tm=1)
10:                 new.safe_eval(code, tm)
11:         end

Public Instance methods

Eval code in closed environment within tm sec. If over the tm sec for running, raise TimeoutError.

tm秒の中の閉じている環境におけるEvalコード 走るためのtm秒の間TimeoutErrorを上げてください。

    # File lib/safe_eval.rb, line 18
18:         def safe_eval(code, tm=1)
19:                 result = nil
20:                 tg = nil
21:                 th = Thread.start do
22:                         # スレッドグループを作り、
23:                         # 新たなスレッドはすべてこれに所属させる。
24:                         tg = ThreadGroup.new.add(Thread.current)
25:                         $SAFE  = 4
26:                         result = Module.new.instance_eval(code)
27:                         true
28:                 end.join(tm)
29:                 # 生成されたスレッドをすべて削除
30:                 tg.list.each {|t| t.kill }
31:                 raise TimeoutError unless th # タイムアウトした場合 Thread は nil を返す
32:                 result
33:         end