class ArgumentError

Public Class Methods

block_required!(block) click to toggle source
# File lib/gorillib/core_ext/exception.rb, line 106
def self.block_required!(block)
  raise self.new("Block is required") unless block
end
check_type!(obj, types, *args) click to toggle source

@param obj [Object] Object to check @param types [Array] Types or methods to compare

@example simple

TypeMismatchError.mismatched!(:foo)
  #=> "TypeMismatchError: :foo has mismatched type

@example Can supply the types or duck-types that are expected:

TypeMismatchError.mismatched!(:foo, [:to_str, Integer])
  #=> "TypeMismatchError: :foo has mismatched type; expected #to_str or Integer"
# File lib/gorillib/core_ext/exception.rb, line 122
def self.check_type!(obj, types, *args)
  types = Array(types)
  return true if types.any? do |type|
    case type
    when Module then obj.is_a?(type)
    when Symbol then obj.respond_to?(type)
    else raise StandardError, "Can't check type #{type} -- this is an error in the call to the type-checker, not in the object the type-checker is checking"
    end
  end
  self.mismatched!(obj, types, *args)
end
mismatched!(obj, types=[], msg=nil, *args) click to toggle source

@param [Array] types

@example simple

TypeMismatchError.mismatched!(:foo)
  #=> "TypeMismatchError: :foo has mismatched type

@example Can supply the types or duck-types that are expected:

TypeMismatchError.mismatched!(:foo, [:to_str, Integer])
  #=> "TypeMismatchError: :foo has mismatched type; expected #to_str or Integer"
# File lib/gorillib/core_ext/exception.rb, line 94
def self.mismatched!(obj, types=[], msg=nil, *args)
  types = Array(types)
  message = (obj.inspect rescue '(uninspectable object)')
  message << " has mismatched type"
  message << ': ' << msg if msg
  unless types.empty?
    message << '; expected ' << types.map{|type| type.is_a?(Symbol) ? "##{type}" : type.to_s }.join(" or ")
  end
  raise self, message, *args
end