Module: CMDx::Coercions::Complex

Extended by:
Complex
Included in:
Complex
Defined in:
lib/cmdx/coercions/complex.rb

Overview

Coerces to ‘Complex`. Supply `imaginary:` to provide the imaginary component when `value` is a real-only input.

Instance Method Summary collapse

Instance Method Details

#call(value, options = EMPTY_HASH) ⇒ Complex, Coercions::Failure

Parameters:

  • value (Object)
  • options (Hash{Symbol => Object}) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :imaginary (Numeric) — default: 0

Returns:



15
16
17
18
19
20
21
22
# File 'lib/cmdx/coercions/complex.rb', line 15

def call(value, options = EMPTY_HASH)
  return value if value.is_a?(::Complex)

  Complex(value, options[:imaginary] || 0)
rescue ArgumentError, TypeError
  type = I18nProxy.t("cmdx.types.complex")
  Failure.new(I18nProxy.t("cmdx.coercions.into_a", type:))
end