class Cmds::Result

A simple data structure returned from calling {Cmds#capture} on a {Cmds} instance.

It contains the exit status code, standard output and standard error, as well as the actual string command issued (after all substitutions).

Instances also have a few convenience methods.

Attributes

cmd[R]

The command string that was executed.

@return [String]

err[R]

The command process' standard error.

@return [String]

out[R]

The command process' standard output.

@return [String]

status[R]

The command process' exit status code.

@return [Fixnum]

Public Class Methods

new(cmd, status, out, err) click to toggle source

@param cmd [String] {#cmd} attribute. @param status [Fixnum] {#status} attribute. @param out [String] {#out} attribute. @param err [String] {#err} attribute.

# File lib/cmds/result.rb, line 45
def initialize cmd, status, out, err
  @cmd = cmd
  @status = status
  @out = out
  @err = err
end

Public Instance Methods

assert() click to toggle source

Raises an error if the command failed (exited with a {#status} other than `0`).

@return [Result] it's self (so that it can be chained).

@raise [SystemCallError] if the command failed.

# File lib/cmds/result.rb, line 74
def assert
  Cmds.check_status @cmd, @status, @err
  self
end
error?() click to toggle source

@return [Boolean]

`true` if {#status} is not `0`.
# File lib/cmds/result.rb, line 62
def error?
  ! ok?
end
ok?() click to toggle source

@return [Boolean]

`true` if {#status} is `0`.
# File lib/cmds/result.rb, line 55
def ok?
  @status == 0
end
to_h() click to toggle source

Get a {Hash} containing the instance variable values for easy logging, JSON dumping, etc.

@example

Cmds( "echo %s", "hey" ).to_h
# => {:cmd=>"echo hey", :status=>0, :out=>"hey\n", :err=>""}

@return [Hash<Symbol, V>]

# File lib/cmds/result.rb, line 89
def to_h
  instance_variables.map { |name|
    [name.to_s.sub('@', '').to_sym, instance_variable_get( name )]
  }.to_h
end