Skip to content

Commit eb17679

Browse files
committed
[GR-17457] Undef Rational.new to avoid public use.
PullRequest: truffleruby/2855
2 parents 1b6a1d4 + 54711f3 commit eb17679

File tree

8 files changed

+24
-15
lines changed

8 files changed

+24
-15
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ def rb_Rational(num, den)
13101310
end
13111311

13121312
def rb_rational_raw(num, den)
1313-
Rational.new(num, den)
1313+
Rational.__send__(:new_already_canonical, num, den)
13141314
end
13151315

13161316
def rb_rational_new(num, den)

spec/ruby/core/rational/rational_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
it "includes Comparable" do
55
Rational.include?(Comparable).should == true
66
end
7+
8+
it "does not respond to new" do
9+
-> { Rational.new(1) }.should raise_error(NoMethodError)
10+
end
711
end

src/main/ruby/truffleruby/core/complex.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def ** (other)
197197
z
198198
else
199199
if defined? Rational
200-
(Rational.new(1, 1) / self) ** -other
200+
(Rational.__send__(:new_already_canonical, 1, 1) / self) ** -other
201201
else
202202
self ** Float(other)
203203
end

src/main/ruby/truffleruby/core/float.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def rationalize(eps=undefined)
134134
f = Math.ldexp(f, Float::MANT_DIG).to_i
135135
n -= Float::MANT_DIG
136136

137-
Rational.new(2 * f, 1 << (1 - n)).rationalize(Rational.new(1, 1 << (1 - n)))
137+
Rational.__send__(:new_already_canonical, 2 * f, 1 << (1 - n)).rationalize(Rational.__send__(:new_already_canonical, 1, 1 << (1 - n)))
138138
else
139139
to_r.rationalize(eps)
140140
end

src/main/ruby/truffleruby/core/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def **(o)
5151
if (Primitive.object_kind_of?(o, Float) || Primitive.object_kind_of?(o, Rational)) && self < 0 && o != o.round
5252
return Complex.new(self, 0) ** o
5353
elsif Primitive.object_kind_of?(o, Integer) && o < 0
54-
return Rational.new(self, 1) ** o
54+
return Rational.__send__(:new_already_canonical, self, 1) ** o
5555
elsif Primitive.object_kind_of?(o, Integer) && o > 0
5656
return self ** o.to_f
5757
end

src/main/ruby/truffleruby/core/rational.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class Rational < Numeric
3838
attr_reader :numerator
3939
attr_reader :denominator
4040

41+
class << self
42+
alias_method :new_already_canonical, :new
43+
undef_method :new
44+
private :new_already_canonical
45+
end
46+
4147
def *(other)
4248
case other
4349
when Rational
@@ -67,19 +73,19 @@ def **(other)
6773
raise ZeroDivisionError, 'divided by 0' if self == 0
6874
Rational(@denominator ** -other, @numerator ** -other)
6975
elsif other == 0
70-
Rational.new(1, 1)
76+
Rational.__send__(:new_already_canonical, 1, 1)
7177
end
7278
else
7379
if self == 0
7480
if other < 0
7581
raise ZeroDivisionError, 'divided by 0'
7682
elsif other > 0
77-
Rational.new(0, 1)
83+
Rational.__send__(:new_already_canonical, 0, 1)
7884
end
7985
elsif self == 1
80-
Rational.new(1, 1)
86+
Rational.__send__(:new_already_canonical, 1, 1)
8187
elsif self == -1
82-
Rational.new(other.even? ? 1 : -1, 1)
88+
Rational.__send__(:new_already_canonical, other.even? ? 1 : -1, 1)
8389
else
8490
to_f ** other
8591
end
@@ -176,7 +182,7 @@ def ==(other)
176182
def abs
177183
return self if @numerator >= 0
178184

179-
Rational.new(-@numerator, @denominator)
185+
Rational.__send__(:new_already_canonical, -@numerator, @denominator)
180186
end
181187

182188
def ceil(precision = 0)
@@ -190,7 +196,7 @@ def ceil(precision = 0)
190196
def coerce(other)
191197
case other
192198
when Integer
193-
[Rational.new(other, 1), self]
199+
[Rational.__send__(:new_already_canonical, other, 1), self]
194200
when Float
195201
[other, self.to_f]
196202
when Rational
@@ -263,7 +269,7 @@ def rationalize(eps = undefined)
263269
end
264270

265271
# The rational number is guaranteed to be in lowest terms.
266-
Rational.new(c * p1 + p0, c * q1 + q0)
272+
Rational.__send__(:new_already_canonical, c * p1 + p0, c * q1 + q0)
267273
end
268274

269275
def round(precision = 0, half: nil)
@@ -373,7 +379,7 @@ def self.reduce(num, den)
373379
end
374380

375381
if den == 1
376-
return new(num, den)
382+
return Rational.__send__(:new_already_canonical, num, den)
377383
end
378384
when Numeric
379385
den = den.to_i
@@ -385,7 +391,7 @@ def self.reduce(num, den)
385391
num = num / gcd
386392
den = den / gcd
387393

388-
new(num, den)
394+
Rational.__send__(:new_already_canonical, num, den)
389395
end
390396
private_class_method :reduce
391397

src/main/ruby/truffleruby/core/rationalizer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def convert
5454
ifp, exp = nu.split(/[eE]/)
5555
ip, fp = ifp.split(/\./)
5656

57-
value = Rational.new(ip.to_i, 1)
57+
value = Rational.__send__(:new_already_canonical, ip.to_i, 1)
5858

5959
if fp
6060
ctype = Truffle::CType

test/mri/excludes/Rational_Test.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
exclude :test_marshal_compatibility, "needs investigation"
66
exclude :test_parse, "needs investigation"
77
exclude :test_power_of_0, "needs investigation"
8-
exclude :test_ruby19, "needs investigation"
98
exclude :test_supp, "needs investigation"
109
exclude :test_to_f, "needs investigation"
1110
exclude :test_Rational_without_exception, "needs investigation"

0 commit comments

Comments
 (0)