Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cfde2af

Browse files
committedAug 25, 2023
Fix {Rational,Complex}#{marshal_dump,marshal_load} to be compatible with CRuby
* Fixes #3228
1 parent 25e9f80 commit cfde2af

File tree

4 files changed

+20
-34
lines changed

4 files changed

+20
-34
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Compatibility:
4242
* Add `Module#const_added` (#3039, @itarato).
4343
* Show the pointer size information (if available) in `FFI::Pointer#inspect` (@nirvdrum).
4444
* Implement performance warnings (`Warning[:performance]`) like in CRuby 3.3 (@eregon).
45+
* The output of `Marshal.dump` is now compatible with CRuby for `Rational` and `Complex` instances (#3228, @eregon).
4546

4647
Performance:
4748

‎spec/ruby/core/marshal/dump_spec.rb

+12-3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ def _dump(level)
271271
end
272272
end
273273

274+
describe "with a Rational" do
275+
it "dumps a Rational" do
276+
Marshal.dump(Rational(2, 3)).should == "\x04\bU:\rRational[\ai\ai\b"
277+
end
278+
end
279+
280+
describe "with a Complex" do
281+
it "dumps a Complex" do
282+
Marshal.dump(Complex(2, 3)).should == "\x04\bU:\fComplex[\ai\ai\b"
283+
end
284+
end
285+
274286
describe "with a String" do
275287
it "dumps a blank String" do
276288
Marshal.dump("".force_encoding("binary")).should == "\004\b\"\000"
@@ -769,7 +781,6 @@ def finalizer.noop(_)
769781
end
770782

771783
describe "when passed an IO" do
772-
773784
it "writes the serialized data to the IO-Object" do
774785
(obj = mock('test')).should_receive(:write).at_least(1)
775786
Marshal.dump("test", obj)
@@ -792,8 +803,6 @@ def finalizer.noop(_)
792803
obj.should_receive(:binmode).at_least(1)
793804
Marshal.dump("test", obj)
794805
end
795-
796-
797806
end
798807

799808
describe "when passed a StringIO" do

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -311,23 +311,14 @@ def fdiv(other)
311311
self / other
312312
end
313313

314-
def marshal_dump
315-
ary = [real, imag]
316-
instance_variables.each do |ivar|
317-
ary.instance_variable_set(ivar, instance_variable_get(ivar))
318-
end
319-
ary
314+
private def marshal_dump
315+
[@real, @imag]
320316
end
321-
private :marshal_dump
322317

323-
def marshal_load(ary)
318+
private def marshal_load(ary)
324319
@real, @imag = ary
325-
ary.instance_variables.each do |ivar|
326-
instance_variable_set(ivar, ary.instance_variable_get(ivar))
327-
end
328320
self
329321
end
330-
private :marshal_load
331322

332323
def <=>(other)
333324
if imag == 0 && Primitive.is_a?(other, Numeric)

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

+4-19
Original file line numberDiff line numberDiff line change
@@ -406,31 +406,17 @@ def initialize(num, den)
406406
@denominator = den
407407
Primitive.freeze(self)
408408
end
409-
private :initialize
410409

411-
def marshal_dump
412-
ary = [@numerator, @denominator]
413-
414-
instance_variables.each do |ivar|
415-
ary.instance_variable_set(ivar, instance_variable_get(ivar))
416-
end
417-
418-
ary
410+
private def marshal_dump
411+
[@numerator, @denominator]
419412
end
420-
private :marshal_dump
421413

422-
def marshal_load(ary)
414+
private def marshal_load(ary)
423415
@numerator, @denominator = ary
424-
425-
ary.instance_variables.each do |ivar|
426-
instance_variable_set(ivar, ary.instance_variable_get(ivar))
427-
end
428-
429416
self
430417
end
431-
private :marshal_load
432418

433-
def with_precision(method, n, **kwargs)
419+
private def with_precision(method, n, **kwargs)
434420
raise TypeError, 'not an integer' unless Primitive.is_a?(n, Integer)
435421

436422
p = 10 ** n
@@ -445,5 +431,4 @@ def with_precision(method, n, **kwargs)
445431
r = Rational(wp, p)
446432
n < 1 ? r.to_i : r
447433
end
448-
private :with_precision
449434
end

0 commit comments

Comments
 (0)