Module: Deplate::Safe

Defined in:
lib/deplate/etc.rb

Overview

Based on code by: From: Florian Gross, flgr AT ccan.de Newsgroups: comp.lang.ruby Subject: Re: safe eval? Date: Mon, 10 May 2004 19:52:27 +0200 Message-ID: <[email protected]>

Class Method Summary collapse

Class Method Details

.safe(level, code, sandbox = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/deplate/etc.rb', line 173

def safe(level, code, sandbox=nil)
    error = nil

    begin
        thread = Thread.new do
            $-w = nil
            sandbox ||= Object.new.taint
            yield(sandbox) if block_given?
            $SAFE = level
            eval(code, sandbox.send(:binding))
        end
        value = thread.value
        result = Marshal.load(Marshal.dump(thread.value))
    rescue Exception => error
        error = Marshal.load(Marshal.dump(error))
    end

    return result, error
end