From: "ko1 (Koichi Sasada) via ruby-core" Date: 2026-10-28T21:30:09+00:00 Subject: [ruby-core:127266] [Ruby Feature#22376] Undefine the allocator for Class Issue #22376 has been updated by ko1 (Koichi Sasada). Claude code reports that (I'm not sure it is important or not): --- FYI, this changes the result of `Class.method(:new)`, and code that rebinds it to another class now fails: ```ruby class H; def initialize(x) = (@x = x); end class H2 < H; end Class.method(:new).unbind.bind(H2).call(1) # Ruby 4.0: #

# master: 'UnboundMethod#bind': singleton method called for a different object (TypeError) ``` `Class.method(:new)` used to be `Class#new`; now it is the new singleton method `##new`, which cannot be bound to other classes. I found this in xmlrpc's test (test/test_client.rb:19, `Class.method(:new).unbind.bind(self).call(*args, &block)` to bypass `Net::HTTP.new`): 18 of 20 tests in test_client.rb error on master. xmlrpc's CI has not run since this change, so it does not show there yet. `Class.instance_method(:new).bind_call(klass, ...)` works on both old and new versions, so the fix on the gem side is easy. I'm not sure whether this pattern is common enough to need anything on the Ruby side; gem-codesearch for `Class.method(:new)` may tell. ---------------------------------------- Feature #22376: Undefine the allocator for Class https://bugs.ruby-lang.org/issues/22376#change-119687 * Author: jhawthorn (John Hawthorn) * Status: Open ---------------------------------------- Uninitialized classes, created by `Class.allocate`, are a recurring source of bugs (most recently #22341). Every place that inspects a class's superclass chain has to check whether the class has been initialized, and it's easy to forget one. I'd like to propose that we forbid Class.allocate, as we already do for several other core types (Proc, Method, UnboundMethod, Binding). To do this we: * Undefine the allocator for Class (`rb_undef_alloc_func`) * Replace `Class#initialize` with a Class.new singleton method that allocates and initializes in one step. * Define `Class#dup` and `Class#clone` so that copying a class still works `Class` can't be subclassed, so I don't think we need to worry about calling initialize. The only place I found `Class.allocate` via `gem-codesearch` were copies of ruby/spec, so I don't think forbidding that will cause problems. Before: ```ruby klass = Class.allocate Class.new(klass) # 'Class#initialize': can't inherit uninitialized class (TypeError) klass.superclass # 'Class#superclass': uninitialized class (TypeError) ``` After: ```ruby Class.allocate # undefined method 'allocate' for class Class (NoMethodError) Class.instance_method(:allocate).bind_call(Class) # 'Class#allocate': allocator undefined for Class (TypeError) ``` PR: https://github.com/ruby/ruby/pull/18964 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/