File tree Expand file tree Collapse file tree 5 files changed +54
-2
lines changed
src/main/ruby/truffleruby/core Expand file tree Collapse file tree 5 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ Compatibility:
17
17
* Fix explicitly inherited ` Struct ` subclasses and don't provide ` #members ` method (#3802 , @andrykonchin ).
18
18
* Support Digest plugins (#1390 , @nirvdrum ).
19
19
* Joni has been updated from 2.2.1 to 2.2.6 (@andrykonchin ).
20
+ * Fix numeric coercing when ` #coerce ` method is not public (#3848 , @andrykonchin ).
20
21
21
22
Performance:
22
23
Original file line number Diff line number Diff line change 106
106
-> { @bignum / :symbol } . should raise_error ( TypeError )
107
107
end
108
108
end
109
+
110
+ it "coerces the RHS and calls #coerce" do
111
+ obj = mock ( "integer plus" )
112
+ obj . should_receive ( :coerce ) . with ( 6 ) . and_return ( [ 6 , 3 ] )
113
+ ( 6 / obj ) . should == 2
114
+ end
115
+
116
+ it "coerces the RHS and calls #coerce even if it's private" do
117
+ obj = Object . new
118
+ class << obj
119
+ private def coerce ( n )
120
+ [ n , 3 ]
121
+ end
122
+ end
123
+
124
+ ( 6 / obj ) . should == 2
125
+ end
109
126
end
Original file line number Diff line number Diff line change 40
40
-> { @bignum - :symbol } . should raise_error ( TypeError )
41
41
end
42
42
end
43
+
44
+ it "coerces the RHS and calls #coerce" do
45
+ obj = mock ( "integer plus" )
46
+ obj . should_receive ( :coerce ) . with ( 5 ) . and_return ( [ 5 , 10 ] )
47
+ ( 5 - obj ) . should == -5
48
+ end
49
+
50
+ it "coerces the RHS and calls #coerce even if it's private" do
51
+ obj = Object . new
52
+ class << obj
53
+ private def coerce ( n )
54
+ [ n , 10 ]
55
+ end
56
+ end
57
+
58
+ ( 5 - obj ) . should == -5
59
+ end
43
60
end
Original file line number Diff line number Diff line change @@ -55,4 +55,21 @@ def +(other)
55
55
RUBY
56
56
ruby_exe ( code ) . should == "-1"
57
57
end
58
+
59
+ it "coerces the RHS and calls #coerce" do
60
+ obj = mock ( "integer plus" )
61
+ obj . should_receive ( :coerce ) . with ( 6 ) . and_return ( [ 6 , 3 ] )
62
+ ( 6 + obj ) . should == 9
63
+ end
64
+
65
+ it "coerces the RHS and calls #coerce even if it's private" do
66
+ obj = Object . new
67
+ class << obj
68
+ private def coerce ( n )
69
+ [ n , 3 ]
70
+ end
71
+ end
72
+
73
+ ( 6 + obj ) . should == 9
74
+ end
58
75
end
Original file line number Diff line number Diff line change @@ -179,9 +179,9 @@ def coerce(other)
179
179
180
180
def math_coerce ( other , error = :coerce_error )
181
181
other = Truffle ::Interop . unbox_if_needed ( other )
182
- return math_coerce_error ( other , error ) unless other . respond_to? :coerce
182
+ return math_coerce_error ( other , error ) unless other . respond_to? ( :coerce , true )
183
183
184
- values = other . coerce ( self )
184
+ values = other . __send__ ( :coerce , self )
185
185
186
186
unless Primitive . is_a? ( values , Array ) && values . length == 2
187
187
if error == :no_error
You can’t perform that action at this time.
0 commit comments