Module: TTY::Coercion
- Defined in:
- lib/tty/support/coercion.rb
Overview
A mixin to coerce a value into a specific class.
Instance Method Summary collapse
-
#coerce_to(object, cls, method) ⇒ Object
Helper to coerce value into a specific class.
Instance Method Details
#coerce_to(object, cls, method) ⇒ Object
Helper to coerce value into a specific class.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tty/support/coercion.rb', line 15 def coerce_to(object, cls, method) return object if object.is_a?(cls) begin result = object.__send__(method) rescue Exception => e raise TypeError, "Coercion error #{e.}" end unless result.is_a?(cls) fail TypeError, "Coercion error: obj.#{method} did not return " \ "a #{cls} (was #{result.class})" end result end |