module Algebrick::TypeCheck

noinspection RubyInstanceMethodNamingConvention

Private Class Methods

error(value, message, types) click to toggle source
# File lib/algebrick/type_check.rb, line 53
def self.error(value, message, types)
  raise TypeError,
        "Value (#{value.class}) '#{value}' #{message} any of: #{types.join('; ')}."
end

Public Instance Methods

Child!(value, *types) click to toggle source
# File lib/algebrick/type_check.rb, line 45
def Child!(value, *types)
  Child?(value, *types) or
      TypeCheck.error(value, 'is not child', types)
  value
end
Child?(value, *types) click to toggle source
# File lib/algebrick/type_check.rb, line 40
def Child?(value, *types)
  Type?(value, Class) &&
      types.any? { |t| value <= t }
end
Match!(value, *types) click to toggle source
# File lib/algebrick/type_check.rb, line 34
def Match!(value, *types)
  Match?(value, *types) or
      TypeCheck.error(value, 'is not matching', types)
  value
end
Match?(value, *types) click to toggle source
# File lib/algebrick/type_check.rb, line 30
def Match?(value, *types)
  types.any? { |t| t === value }
end
Type!(value, *types) click to toggle source
# File lib/algebrick/type_check.rb, line 24
def Type!(value, *types)
  Type?(value, *types) or
      TypeCheck.error(value, 'is not', types)
  value
end
Type?(value, *types) click to toggle source

FIND: type checking of collections?

# File lib/algebrick/type_check.rb, line 20
def Type?(value, *types)
  types.any? { |t| value.is_a? t }
end