Class: TrustedSandbox::GeneralPurpose

Inherits:
Object
  • Object
show all
Defined in:
lib/trusted_sandbox/general_purpose.rb

Overview

This is a general purpose class that can be used to run untrusted code in a container using TrustedSandbox. Usage:

TrustedSandbox.run! TrustedSandbox::GeneralPurpose, "1 + 1"
# => 2

TrustedSandbox.run! TrustedSandbox::GeneralPurpose, "input[:a] + input[:b]", input: {a: 1, b: 2}
# => 3

Instance Method Summary collapse

Constructor Details

#initialize(code, args = {}) ⇒ GeneralPurpose

Returns a new instance of GeneralPurpose.



13
14
15
16
17
18
19
20
# File 'lib/trusted_sandbox/general_purpose.rb', line 13

def initialize(code, args={})
  @code = code
  args.each do |name, value|
    singleton_klass = class << self; self; end
    singleton_klass.class_eval { attr_reader name }
    instance_variable_set "@#{name}", value
  end
end

Instance Method Details

#runObject



22
23
24
# File 'lib/trusted_sandbox/general_purpose.rb', line 22

def run
  eval @code
end