Class: Dia::RubyBlock
- Inherits:
-
Object
- Object
- Dia::RubyBlock
- Includes:
- SharedFeatures
- Defined in:
- lib/dia/ruby_block.rb
Overview
The RubyBlock class provides an interface for executing a block of ruby code in a sandbox.
Instance Attribute Summary collapse
-
#exception ⇒ Dia::ExceptionStruct?
(also: #e)
readonly
Provides access to the data of an exception object that has been rescued in the process that was last used to execute a sandbox.
- #redirect_stderr ⇒ Object
- #redirect_stdout ⇒ Object
- #rescue_exception ⇒ Object
-
#stderr ⇒ String?
readonly
Provides access to the Standard Error stream of the process that was last used to execute a sandbox.
-
#stdout ⇒ String?
readonly
Provides access to the Standard Output stream of the process that was last used to execute a sandbox.
Attributes included from SharedFeatures
Instance Method Summary collapse
-
#exception_raised? ⇒ Boolean
deprecated
Deprecated.
This method is deprecated. #exception_rescued? should be used instead.
-
#exception_rescued? ⇒ Boolean
This method will tell you if an exception has been rescued in the process that was last used to execute a sandbox.
-
#initialize(profile, &block) ⇒ Dia::RubyBlock
constructor
Returns an instance of Dia::RubyBlock.
-
#redirect_stderr? ⇒ Boolean
This method will tell you if Standard Error output is being redirected in the process spawned to execute a sandbox.
-
#redirect_stdout? ⇒ Boolean
This method will tell you if Standard Output is being redirected in the process spawned to execute a sandbox.
-
#rescue_exception? ⇒ Boolean
This method will tell you if an exception will be rescued in the process that is spawned to execute a sandbox.
-
#run(*args) ⇒ Fixnum
The run method will spawn a child process and execute the block supplied to the constructor in a sandbox.
-
#run_nonblock(*args) ⇒ Fixnum
An identical, but non-blocking form of #run.
-
#value ⇒ String?
Provides access to the return value of the block that has been supplied to the constructor.
Methods included from SharedFeatures
#exit_status, #running?, #terminate
Constructor Details
#initialize(profile, &block) ⇒ Dia::RubyBlock
Returns an instance of Dia::RubyBlock.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dia/ruby_block.rb', line 98 def initialize(profile, &block) raise(ArgumentError, "It is required that a block be passed to the constructor.\n" \ "Please consult the documentation.") unless block_given? @profile = profile @proc = block @rescue_exception = false @redirect_stdout = false @redirect_stderr = false @pipes = {} end |
Instance Attribute Details
#exception ⇒ Dia::ExceptionStruct? (readonly) Also known as: e
Provides access to the data of an exception object that has been rescued in the process that was last used to execute a sandbox. This feature is disabled by default.
267 268 269 |
# File 'lib/dia/ruby_block.rb', line 267 def exception @exception end |
#redirect_stderr=(boolean) ⇒ void #redirect_stderr ⇒ Object, Boolean
37 38 39 |
# File 'lib/dia/ruby_block.rb', line 37 def redirect_stderr @redirect_stderr end |
#redirect_stdout=(boolean) ⇒ void #redirect_stdout ⇒ Object, Boolean
62 63 64 |
# File 'lib/dia/ruby_block.rb', line 62 def redirect_stdout @redirect_stdout end |
#rescue_exception= ⇒ void #rescue_exception ⇒ Object, Boolean
87 88 89 |
# File 'lib/dia/ruby_block.rb', line 87 def rescue_exception @rescue_exception end |
#stderr ⇒ String? (readonly)
Provides access to the Standard Error stream of the process that was last used to execute a sandbox. This feature is disabled by default.
171 172 173 |
# File 'lib/dia/ruby_block.rb', line 171 def stderr @stderr end |
#stdout ⇒ String? (readonly)
Provides access to the Standard Output stream of the process that was last used to execute a sandbox. This feature is disabled by default.
126 127 128 |
# File 'lib/dia/ruby_block.rb', line 126 def stdout @stdout end |
Instance Method Details
#exception_raised? ⇒ Boolean
This method is deprecated. #exception_rescued? should be used instead.
225 226 227 228 229 |
# File 'lib/dia/ruby_block.rb', line 225 def exception_raised? $stderr.puts 'WARNING: Dia::RubyBlock#exception_raised? is deprecated and will be ' \ 'removed from Dia in a future release.' !!exception end |
#exception_rescued? ⇒ Boolean
This method will tell you if an exception has been rescued in the process that was last used to execute a sandbox.
217 218 219 |
# File 'lib/dia/ruby_block.rb', line 217 def exception_rescued? !!exception end |
#redirect_stderr? ⇒ Boolean
This method will tell you if Standard Error output is being redirected in the process spawned to execute a sandbox.
198 199 200 |
# File 'lib/dia/ruby_block.rb', line 198 def redirect_stderr? !!@redirect_stderr end |
#redirect_stdout? ⇒ Boolean
This method will tell you if Standard Output is being redirected in the process spawned to execute a sandbox.
153 154 155 |
# File 'lib/dia/ruby_block.rb', line 153 def redirect_stdout? !!@redirect_stdout end |
#rescue_exception? ⇒ Boolean
This method will tell you if an exception will be rescued in the process that is spawned to execute a sandbox.
242 243 244 |
# File 'lib/dia/ruby_block.rb', line 242 def rescue_exception? !!@rescue_exception end |
#run(*args) ⇒ Fixnum
The run method will spawn a child process and execute the block supplied to the constructor in a sandbox.
This method will block. See #run_nonblock for the non-blocking form of this method.
**Side Effects:**
-
When this method is called, it will reset the instance variables returned by #exception, #stdout, #stderr, and #value to nil.
316 317 318 319 320 321 322 |
# File 'lib/dia/ruby_block.rb', line 316 def run(*args) launch(*args) # parent .. _, @exit_status = Process.wait2(@pid) @pid end |
#run_nonblock(*args) ⇒ Fixnum
An identical, but non-blocking form of #run.
326 327 328 329 330 331 |
# File 'lib/dia/ruby_block.rb', line 326 def run_nonblock(*args) launch(*args) @exit_status = Process.detach(@pid) @pid end |
#value ⇒ String?
Provides access to the return value of the block that has been supplied to the constructor.
285 286 287 288 289 290 291 292 |
# File 'lib/dia/ruby_block.rb', line 285 def value if pipes_readable?(@pipes[:return_reader], @pipes[:return_writer]) @pipes[:return_writer].close @return = @pipes[:return_reader].read @pipes[:return_reader].close end @return end |