Module: CMDx::Coercions::Complex

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

Overview

Converts various input types to Complex number format

Handles conversion from numeric strings, integers, floats, and other values that can be converted to Complex using Ruby’s Complex() method.

Instance Method Summary collapse

Instance Method Details

#call(value, options = {}) ⇒ Complex

Converts a value to a Complex number

Examples:

Convert numeric strings to Complex

Complex.call("3+4i")                     # => (3+4i)
Complex.call("2.5")                      # => (2.5+0i)

Convert other numeric types

Complex.call(5)                          # => (5+0i)
Complex.call(3.14)                       # => (3.14+0i)
Complex.call(Complex(1, 2))              # => (1+2i)

Parameters:

  • value (Object)

    The value to convert to Complex

  • options (Hash) (defaults to: {})

    Optional configuration parameters (currently unused)

Options Hash (options):

  • :* (Object)

    Any configuration option (unused)

Returns:

  • (Complex)

    The converted Complex number value

Raises:

  • (CoercionError)

    If the value cannot be converted to Complex



32
33
34
35
36
37
# File 'lib/cmdx/coercions/complex.rb', line 32

def call(value, options = {})
  Complex(value)
rescue ArgumentError, TypeError
  type = Locale.t("cmdx.types.complex")
  raise CoercionError, Locale.t("cmdx.coercions.into_a", type:)
end