Project

General

Profile

Bug #13358

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

In https://github.com/ruby/ruby/commit/15960b37e82ba60455c480b1c23e1567255d3e05 OpenStruct gained 

 ~~~ruby ~~~ 
   class << self # :nodoc: 
     alias allocate new 
   end 
 ~~~ 

 Which is rather severely conflicting with expected behavior as `Class.allocate` Class.allocate is meant to [not call initialize](http://ruby-doc.org/core-2.4.0/Class.html#method-i-allocate). So, in fact, the change made `allocate` allocate of `OpenStruct` OpenStruct do what `allocate` allocate is asserting not to do :-/ 

 For `OpenStruct` OpenStruct itself that isn't that big a deal, for classes inheriting from `OpenStruct` OpenStruct it breaks `allocate` allocate though. 

 Example: 

 ~~~ruby ~~~ 
 require 'ostruct' 

 class A < OpenStruct 
   def initialize(x, y = {}) 
     super(y) 
   end 
 end 

 A.allocate 
 ~~~ 

 As `allocate` is alias'd to `new` in `OpenStruct` this will attempt to initialize `A` which will raise an `ArgumentError` because `A` cannot be initialized without arguments. 

 ~~~ 
 $ ruby x.rb 
 x.rb:4:in `initialize': wrong number of arguments (given 0, expected 1..2) (ArgumentError) 
         from x.rb:9:in `new' 
         from x.rb:9:in `<main>' 
 ~~~ 

 OpenStruct at the very least should document the fact that its allocate is behaving differently. 
 Ideally, `OpenStruct` OpenStruct should not alias allocate at all. 

Back