Skip to content

Fewer duplicate specs for Kernel methods#1376

Merged
eregon merged 1 commit into
ruby:masterfrom
Earlopain:kernel-methods
Jun 12, 2026
Merged

Fewer duplicate specs for Kernel methods#1376
eregon merged 1 commit into
ruby:masterfrom
Earlopain:kernel-methods

Conversation

@Earlopain

Copy link
Copy Markdown
Contributor

Currently it checks that Kernel.method == Object#method by running specs twice. But the Kernel methods are just mixed into Object, so we should be able to assert that instead.

Draft for now. I think this still asserts the same thing, but do let me know if I should do the same for the other kernel methods/if I'm missing something/it can be asserted in a better way.

Comment thread core/object/object_spec.rb Outdated
Comment thread core/object/object_spec.rb Outdated
Comment thread core/kernel/Float_spec.rb Outdated
@eregon

eregon commented Jun 11, 2026

Copy link
Copy Markdown
Member

This seems a nice simplification, most Kernel methods are just module_function, so both singleton and private instance methods of Kernel.

There seems to be no easy way to prove they are the same, except maybe via their .source_location:

irb(main):001> Kernel.instance_method(:Float) == Kernel.singleton_class.instance_method(:Float)
=> false
irb(main):002> Kernel.instance_method(:Float).source_location
=> ["<internal:kernel>", 194]
irb(main):003> Kernel.instance_method(:Float).source_location == Kernel.singleton_class.instance_method(:Float).source_location
=> true

That's only OK if the returned source_location is non-nil though.
Good enough in this case since it seems the case on Ruby 3.3+, but I guess it won't be the case for most other Kernel methods (e.g. for Kernel#p).
So I wouldn't bother to go this way since it works for a small subset.

I think for Kernel methods it's fine enough to check that there is a private instance method and a public singleton method of the same name, and only test one of them, and assume the other with the same name does the same.

So here we'd test only Kernel#Float (because it's what is most used), and just check that Kernel#Float is a private instance method, and that Kernel.Float is a public singleton method.
And we can do the same for all other module_function methods of Kernel.

@Earlopain

Copy link
Copy Markdown
Contributor Author

Turns out that most already only tested a single variant. It's about 700 fewer specs overall, which I assume were mostly caused by sprintf.

I've added private/public checks to all, which is the only observable effect of module_function. I did try (again) to prove they are identical but failed (again). One could also check if arity/parameters/source_location are the identical but I don't think that gives much further value.

@Earlopain Earlopain marked this pull request as ready for review June 12, 2026 17:24

@eregon eregon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great cleanup, thank you, just a few nits

Comment thread core/kernel/fixtures/classes.rb Outdated
Comment thread core/kernel/autoload_spec.rb
Comment thread core/kernel/chomp_spec.rb Outdated
Currently, some specs are run for both instance and singleton method

This moves specs to run just on instance methods,
and simply checks if the singleton method exists
@eregon eregon merged commit a1f57e8 into ruby:master Jun 12, 2026
14 checks passed
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