Module: Type

Defined in:
lib/pegarus/compatibility.rb

Class Method Summary collapse

Class Method Details

.coerce_to(obj, cls, meth) ⇒ Object

Raises:

  • (TypeError)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pegarus/compatibility.rb', line 11

def self.coerce_to(obj, cls, meth)
  return obj if obj.kind_of? cls

  begin
    ret = obj.__send__(meth)
  rescue Exception
    raise TypeError, "Coercion error: #{obj.inspect}.#{meth} => #{cls} failed"
  end

  return ret if ret.kind_of? cls

  raise TypeError, "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{ret.class})"
end