Module: BTC::Safety

Included in:
BTC
Defined in:
lib/btcruby/safety.rb

Overview

Several functions intended to detect bad data in runtime and throw exceptions. These are for programmer’s errors, not for bad user input. Bad user input should never raise exceptions.

Instance Method Summary collapse

Instance Method Details

#AssertType(value, type) ⇒ Object



7
8
9
10
11
# File 'lib/btcruby/safety.rb', line 7

def AssertType(value, type)
  if !value.is_a?(type)
    raise ArgumentError, "Value #{value.inspect} must be of type #{type}!"
  end
end

#AssertTypeOrNil(value, type) ⇒ Object



12
13
14
15
# File 'lib/btcruby/safety.rb', line 12

def AssertTypeOrNil(value, type)
  return if value == nil
  AssertType(value, type)
end

#Invariant(condition, message) ⇒ Object

Checks invariant and raises an exception.



18
19
20
21
22
# File 'lib/btcruby/safety.rb', line 18

def Invariant(condition, message)
  if !condition
    raise RuntimeError, "BTC Invariant Failure: #{message}"
  end
end