Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@der-scheme
Copy link

Adds Complex#deconstruct as an alias for Complex#rect.

Reasoning and usefulness

  • I am not aware of complex literals with real parts, mandating the use of the pin operator in patterns (and only for exact comparisons).
  • The Syntax of complex array patterns is identical to Kernel#Complex.
  • It allows constraints on the real and imaginary parts of the number.
  • Complex already implements the functionality as rect, this just makes it available to patterns.

Usage

Complex(1, 2) in Complex(1, 2)  # => true (false in Ruby < 4)

# Assert that a Complex is, in fact, a real number and extract the real part.
num = Complex(42, 0)            # => (42+0i)
num => Complex(real, 0)         # => nil
real                            # => 42

Issues

  • Is the test sufficient and in the right place?

Alias for `Complex#rect`.
This is useful because:
* I am not aware of complex literals with real parts, mandating the use of the pin operator in patterns (and only for exact comparisons).
* The Syntax of complex array patterns is identical to `Kernel#Complex`.
* It allows constraints on the real and imaginary parts of the number.
@der-scheme der-scheme force-pushed the add_complex_deconstruct branch from 6a101b9 to 8f9a36a Compare November 12, 2025 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant