Skip to content

Commit 31c30b2

Browse files
authored
Complex comparisons work differently in Ruby 2.7 (#214)
* Complex comparisons work differently in Ruby 2.7 Fixes #213
1 parent 6cbbb7d commit 31c30b2

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ language: ruby
33
cache: bundler
44
dist: xenial
55
rvm:
6+
- 2.7
67
- 2.6
78
- 2.5
89
- 2.4

spec/ruby_units/complex_spec.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
require File.dirname(__FILE__) + '/../spec_helper'
22

3-
# Complex numbers are a bit strange
4-
# Technically you can't really compare them using <=>, and ruby 1.9 does not implement this method for them
5-
# so it stands to reason that complex units should also not support :> or :<
6-
73
describe Complex do
84
subject { Complex(1, 1) }
95
it { is_expected.to respond_to :to_unit }
106
end
117

8+
# Complex numbers are a bit strange. Technically you can't really compare them
9+
# using :<=>, and Ruby < 2.7 does not implement this method for them so it stands
10+
# to reason that complex units should also not support :> or :<.
11+
#
12+
# This inconsistency was corrected in Ruby 2.7.
13+
# @see https://rubyreferences.github.io/rubychanges/2.7.html#complex
14+
# @see https://bugs.ruby-lang.org/issues/15857
1215
describe 'Complex Unit' do
1316
subject { Complex(1.0, -1.0).to_unit }
1417

@@ -19,7 +22,12 @@
1922
it { is_expected.to be === '1-1i'.to_unit }
2023

2124
it 'is not comparable' do
22-
expect { subject > '1+1i'.to_unit }.to raise_error(NoMethodError)
23-
expect { subject < '1+1i'.to_unit }.to raise_error(NoMethodError)
25+
if subject.scalar.respond_to?(:<=>) # this is true for Ruby >= 2.7
26+
expect { subject > '1+1i'.to_unit }.to raise_error(ArgumentError)
27+
expect { subject < '1+1i'.to_unit }.to raise_error(ArgumentError)
28+
else
29+
expect { subject > '1+1i'.to_unit }.to raise_error(NoMethodError)
30+
expect { subject < '1+1i'.to_unit }.to raise_error(NoMethodError)
31+
end
2432
end
2533
end

0 commit comments

Comments
 (0)