diff --git a/CHANGELOG.md b/CHANGELOG.md index ed0e5434583d..5054b87ec85d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Compatibility: * `Errno` constants with the same `errno` number are now the same class. * Implement `Enumerable#tally` and `Enumerable#filter_map` (#2144 and #2152, @LillianZ). * Implement `Range#minmax`. +* Implement `Complex#<=>` (#2004, @ssnickolay). Performance: diff --git a/spec/tags/core/complex/comparision_tags.txt b/spec/tags/core/complex/comparision_tags.txt deleted file mode 100644 index c9f13ac819cb..000000000000 --- a/spec/tags/core/complex/comparision_tags.txt +++ /dev/null @@ -1,3 +0,0 @@ -fails:Complex#<=> returns nil if either self or argument has imaginary part -fails:Complex#<=> returns nil if argument is not numeric -fails:Complex#<=> returns 0, 1, or -1 if self and argument do not have imaginary part diff --git a/src/main/ruby/truffleruby/core/complex.rb b/src/main/ruby/truffleruby/core/complex.rb index 35e306f7b729..cdcfadd8d580 100644 --- a/src/main/ruby/truffleruby/core/complex.rb +++ b/src/main/ruby/truffleruby/core/complex.rb @@ -36,7 +36,7 @@ class Complex < Numeric - undef_method :%, :<, :<=, :<=>, :>, :>=, :between?, :clamp, # comparable + undef_method :%, :<, :<=, :>, :>=, :between?, :clamp, # comparable :div, :divmod, :floor, :ceil, :modulo, :remainder, :round, :step, :truncate, :i, :negative?, :positive? @@ -373,6 +373,16 @@ def marshal_load(ary) self end + def <=>(other) + if imag == 0 && other.kind_of?(Numeric) + if other.kind_of?(Complex) && other.imag == 0 + real <=> other.real + elsif other.real? + real <=> other + end + end + end + I = Complex(0, 1) end diff --git a/test/mri/excludes/Complex_Test.rb b/test/mri/excludes/Complex_Test.rb index 08224e700937..fa44cc4489db 100644 --- a/test/mri/excludes/Complex_Test.rb +++ b/test/mri/excludes/Complex_Test.rb @@ -16,4 +16,3 @@ exclude :test_rationalize, "needs investigation" exclude :test_Complex_with_invalid_exception, "needs investigation" exclude :test_conv, "needs investigation" -exclude :test_cmp, "needs investigation"