Module: Common

Defined in:
lib/debugtrace/common.rb

Overview

 Defines commonly used functions.

Class Method Summary collapse

Class Method Details

.check_type(value_name, value, type) ⇒ String

Check the value types.

Parameters:

  • value_name (String)

    the value name

  • value (String)

    the value

  • type (Class)

    the type

Returns:

  • (String)

    the value

Raises:

  • (TypeError)

    if the value is not an instance of the type or the subclass of the type



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/debugtrace/common.rb', line 14

def self.check_type(value_name, value, type)
  raise TypeError, "Argument value_name (=#{value_name}) must be a String" unless value_name.is_a?(String)
  raise TypeError, "Argument type (=#{type}) must be a Class" unless type.is_a?(Class)

  error = false
  if type == FalseClass || type == TrueClass
    # false or true
    if value.class != FalseClass && value.class != TrueClass
      error = true
    end
  else
    error = !value.is_a?(type)
  end

  if error
    value_string = value.instance_of?(String) ? "\"#{value}\"" : "#{value}"
    top_type_name = type.name.slice(0).upcase
    a = top_type_name == 'A' || top_type_name == 'I' || top_type_name == 'U' ||
      top_type_name == 'E' || top_type_name == 'O' ? 'an' : 'a'
    raise TypeError, "Argument #{value_name} (=#{value_string}) must be #{a} #{type}"
  end
  return value
end