Skip to content

Commit d25ca2c

Browse files
andrykonchineregon
authored andcommitted
Fix Rational() with Complex second argument
1 parent f45e328 commit d25ca2c

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

core/kernel/Rational_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,24 @@
7676
end
7777

7878
describe "when passed a Complex" do
79-
it "returns a Rational from the real part if the imaginary part is 0" do
80-
Rational(Complex(1, 0)).should == Rational(1)
79+
context "[Complex]" do
80+
it "returns a Rational from the real part if the imaginary part is 0" do
81+
Rational(Complex(1, 0)).should == Rational(1)
82+
end
83+
84+
it "raises a RangeError if the imaginary part is not 0" do
85+
-> { Rational(Complex(1, 2)) }.should raise_error(RangeError, "can't convert 1+2i into Rational")
86+
end
8187
end
8288

83-
it "raises a RangeError if the imaginary part is not 0" do
84-
-> { Rational(Complex(1, 2)) }.should raise_error(RangeError)
89+
context "[Numeric, Complex]" do
90+
it "uses the real part if the imaginary part is 0" do
91+
Rational(1, Complex(2, 0)).should == Rational(1, 2)
92+
end
93+
94+
it "divides a numerator by the Complex denominator if the imaginary part is not 0" do
95+
Rational(1, Complex(2, 1)).should == Complex(2/5r, -1/5r)
96+
end
8597
end
8698
end
8799

0 commit comments

Comments
 (0)