Class: SystemCall::Result

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/system_call/result.rb

Overview

A class that represents a result of a system call.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exit_status, success_result, error_result) ⇒ Result

Initializes a new SystemCall::Result.

Parameters:

  • exit_status (Process::Status)

    The exit status of the result.

  • success_result (String)

    The STDOUT of the result.

  • error_result (String)

    The STDERR of the result.



24
25
26
27
28
# File 'lib/system_call/result.rb', line 24

def initialize(exit_status, success_result, error_result)
  @exit_status = exit_status
  @success_result = success_result
  @error_result = error_result
end

Instance Attribute Details

#error_resultString (readonly)

Returns The STDERR of the result.

Returns:

  • (String)

    The STDERR of the result.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/system_call/result.rb', line 13

class Result
  include Comparable

  attr_reader :exit_status, :success_result, :error_result

  ##
  # Initializes a new {Result}.
  #
  # @param exit_status [Process::Status] The exit status of the result.
  # @param success_result [String] The STDOUT of the result.
  # @param error_result [String] The STDERR of the result.
  def initialize(exit_status, success_result, error_result)
    @exit_status = exit_status
    @success_result = success_result
    @error_result = error_result
  end

  ##
  # Gets the exit code of the process.
  #
  # @return [Integer]
  def exit_code
    exit_status.exitstatus
  end

  ##
  # Indicates whether the command has been executed successfully.
  #
  # @return [Boolean]
  def success?
    exit_status.success?
  end

  ##
  # Indicates whether the command has not been executed successfully.
  #
  # @return [Boolean]
  def error?
    !success?
  end

  ##
  # Returns the command result.
  #
  # @return [String] Returns result from {#success_result} when command exited
  #   successfully. Otherwise returns result from {#error_result}.
  def result
    success? ? success_result : error_result
  end

  alias to_s result

  ##
  # Compares the result with the result of {#to_s} from another object.
  #
  # @param other [#to_s, nil]
  # @return [Integer, nil]
  def <=>(other)
    return nil if other.nil?

    result <=> other.to_s
  end
end

#exit_statusProcess::Status (readonly)

Returns The exit status of the result.

Returns:

  • (Process::Status)

    The exit status of the result.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/system_call/result.rb', line 13

class Result
  include Comparable

  attr_reader :exit_status, :success_result, :error_result

  ##
  # Initializes a new {Result}.
  #
  # @param exit_status [Process::Status] The exit status of the result.
  # @param success_result [String] The STDOUT of the result.
  # @param error_result [String] The STDERR of the result.
  def initialize(exit_status, success_result, error_result)
    @exit_status = exit_status
    @success_result = success_result
    @error_result = error_result
  end

  ##
  # Gets the exit code of the process.
  #
  # @return [Integer]
  def exit_code
    exit_status.exitstatus
  end

  ##
  # Indicates whether the command has been executed successfully.
  #
  # @return [Boolean]
  def success?
    exit_status.success?
  end

  ##
  # Indicates whether the command has not been executed successfully.
  #
  # @return [Boolean]
  def error?
    !success?
  end

  ##
  # Returns the command result.
  #
  # @return [String] Returns result from {#success_result} when command exited
  #   successfully. Otherwise returns result from {#error_result}.
  def result
    success? ? success_result : error_result
  end

  alias to_s result

  ##
  # Compares the result with the result of {#to_s} from another object.
  #
  # @param other [#to_s, nil]
  # @return [Integer, nil]
  def <=>(other)
    return nil if other.nil?

    result <=> other.to_s
  end
end

#success_resultString (readonly)

Returns The STDOUT of the result.

Returns:

  • (String)

    The STDOUT of the result.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/system_call/result.rb', line 13

class Result
  include Comparable

  attr_reader :exit_status, :success_result, :error_result

  ##
  # Initializes a new {Result}.
  #
  # @param exit_status [Process::Status] The exit status of the result.
  # @param success_result [String] The STDOUT of the result.
  # @param error_result [String] The STDERR of the result.
  def initialize(exit_status, success_result, error_result)
    @exit_status = exit_status
    @success_result = success_result
    @error_result = error_result
  end

  ##
  # Gets the exit code of the process.
  #
  # @return [Integer]
  def exit_code
    exit_status.exitstatus
  end

  ##
  # Indicates whether the command has been executed successfully.
  #
  # @return [Boolean]
  def success?
    exit_status.success?
  end

  ##
  # Indicates whether the command has not been executed successfully.
  #
  # @return [Boolean]
  def error?
    !success?
  end

  ##
  # Returns the command result.
  #
  # @return [String] Returns result from {#success_result} when command exited
  #   successfully. Otherwise returns result from {#error_result}.
  def result
    success? ? success_result : error_result
  end

  alias to_s result

  ##
  # Compares the result with the result of {#to_s} from another object.
  #
  # @param other [#to_s, nil]
  # @return [Integer, nil]
  def <=>(other)
    return nil if other.nil?

    result <=> other.to_s
  end
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compares the result with the result of #to_s from another object.

Parameters:

  • other (#to_s, nil)

Returns:

  • (Integer, nil)


70
71
72
73
74
# File 'lib/system_call/result.rb', line 70

def <=>(other)
  return nil if other.nil?

  result <=> other.to_s
end

#error?Boolean

Indicates whether the command has not been executed successfully.

Returns:

  • (Boolean)


50
51
52
# File 'lib/system_call/result.rb', line 50

def error?
  !success?
end

#exit_codeInteger

Gets the exit code of the process.

Returns:

  • (Integer)


34
35
36
# File 'lib/system_call/result.rb', line 34

def exit_code
  exit_status.exitstatus
end

#resultString Also known as: to_s

Returns the command result.

Returns:



59
60
61
# File 'lib/system_call/result.rb', line 59

def result
  success? ? success_result : error_result
end

#success?Boolean

Indicates whether the command has been executed successfully.

Returns:

  • (Boolean)


42
43
44
# File 'lib/system_call/result.rb', line 42

def success?
  exit_status.success?
end