Skip to content

Add half: keyword to Numeric#round - #3230

Open
znz wants to merge 1 commit into
ruby:masterfrom
znz:numeric-round-half
Open

znz wants to merge 1 commit into
ruby:masterfrom
znz:numeric-round-half

Conversation

@znz

@znz znz commented Oct 14, 2026

Copy link
Copy Markdown
Member

Changes

Numeric#round is declared as () -> Integer | (Integer digits) -> Numeric, but it also accepts the half: keyword: numeric.c's num_round just calls flo_round(argc, argv, rb_Float(num)), so every argument is forwarded to Float#round, and the current rdoc call-seq is round(digits = 0, half: :up) -> integer or float.

  • core/numeric.rbs: add ?half: round_mode to both overloads of Numeric#round (the round_mode alias is the one Integer#round already uses) and update the generated call-seq comment.
  • test/stdlib/Numeric_test.rb: add test_round using a Numeric subclass that defines to_f, so the test exercises Numeric#round itself rather than Integer#round.

Measured on Ruby 4.0.7 with class N < Numeric; def to_f; 2.5; end; end:

N.new.round                 # => 3
N.new.round(half: :even)    # => 2
N.new.round(half: :down)    # => 2
N.new.round(1)              # => 2.5
N.new.round(1, half: :even) # => 2.5
N.new.round(0, half: :even) # => 2

Verification

  • bin/test_runner.rb test/stdlib/Numeric_test.rb and rbs validate pass locally on Ruby 3.4.8.

🤖 Generated with Claude Code

@ksss

ksss commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

@znz Thank you but,
We should update the Ruby version to v4.0.7 in comment CI.

Numeric#round passes all of its arguments to Float#round
(numeric.c: num_round calls flo_round(argc, argv, rb_Float(num))),
so it accepts the half: keyword as well. Use the round_mode alias
that Integer#round already uses.

The rdoc call-seq on ruby/ruby master is round(digits = 0, half: :up)
(96311d13c2), but the generated comment follows the Ruby release used
by the RBS Comments workflow, which does not have that fix yet, so the
comment is left as generated.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@znz

znz commented Oct 15, 2026

Copy link
Copy Markdown
Member Author

Thank you for the review. I looked at what the RBS Comments job actually rewrites: the only diff is the call-seq comment line that I had edited by hand (round(digits = 0, half: :up)). That line comes from ruby/ruby master (ruby/ruby@96311d13c2, "[DOC] Add missing half kwags to Numeric#round"), and the commit is in neither v4.0.7 nor the current ruby_4_0 branch, so the job would still regenerate round(digits = 0) after #3235.

I have dropped the hand edit and left the comment as generated; the signature and the test are unchanged. The comment can follow once a Ruby release used by the workflow carries that doc fix. The signature itself holds for 4.0.x: Numeric#round delegates to Float#round in every version, which accepts half:.

🤖 Generated with Claude Code

@znz
znz force-pushed the numeric-round-half branch from 6102342 to 91b8173 Compare October 15, 2026 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants