Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/vm/complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ func registerNumericGeneric(vm *VM, cNumeric *RClass) {
cNumeric.define("real?", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.True
})
// zero? is self == 0, dispatched so a subclass's own #== / #<=> decides. This
// is also what the #nonzero? above relies on.
cNumeric.define("zero?", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return object.Bool(vm.send(self, "==", []object.Value{object.IntValue(0)}, nil).Truthy())
})
// real returns self: a real number is its own real part.
cNumeric.define("real", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
return self
})
// floor / ceil / round / truncate fall back to operating on #to_f, so a Numeric
// subclass that only defines #to_f still rounds; the optional digits argument is
// forwarded to Float's own implementation.
for _, name := range []string{"floor", "ceil", "round", "truncate"} {
m := name
cNumeric.define(m, func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return vm.send(vm.send(self, "to_f", nil, nil), m, args, nil)
})
}
}

// asComplexVal coerces a real number to a Complex (zero imaginary part); a
Expand Down
22 changes: 22 additions & 0 deletions internal/vm/numeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,25 @@ func TestNumericHierarchy(t *testing.T) {
}
}
}

// TestNumericDefaultMethods covers the Numeric fallbacks — zero?, real, floor,
// ceil, round and truncate — that a subclass defining only #to_f (and #<=>)
// inherits, asserted against MRI Ruby 4.0.6.
func TestNumericDefaultMethods(t *testing.T) {
pre := "class FNum < Numeric; def initialize(v); @v = v; end; def v; @v; end; " +
"def <=>(o); @v <=> (o.is_a?(FNum) ? o.v : o); end; def coerce(o); [FNum.new(o), self]; end; " +
"def to_f; @v.to_f; end; end; "
cases := []struct{ src, want string }{
{pre + `p [FNum.new(2.7).truncate, FNum.new(2.7).floor, FNum.new(2.7).ceil, FNum.new(2.7).round]`, "[2, 2, 3, 3]\n"},
{pre + `p [FNum.new(-2.7).truncate, FNum.new(-2.7).floor, FNum.new(-2.7).ceil, FNum.new(-2.7).round]`, "[-2, -3, -2, -3]\n"},
{pre + `p FNum.new(2.75).round(1)`, "2.8\n"},
{pre + `p [FNum.new(0).zero?, FNum.new(5).zero?]`, "[true, false]\n"},
{pre + `p FNum.new(5).real.v`, "5\n"},
{pre + `p [FNum.new(0).nonzero?, FNum.new(5).nonzero?.v]`, "[nil, 5]\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15800
15810