From: Ilias Lazaridis Date: 2011-06-12T23:40:37+09:00 Subject: Re: CORE - Literal Instantiation breaks Object Model On 10 Ιούν, 01:30, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: CORE - Literal Instantiation breaks Object Model" >     on Fri, 10 Jun 2011 03:40:27 +0900, Ilias Lazaridis writes: > > |The behaviour of the class String has been altered, whilst using the > |standard mechanisms of the Object Model. > | > |To my huge surprise, although the li_string has been instantiated as > |an object of class String, the new initialize method was not called. > | > |This is essentially a defect, as the consistency of the Object Model > |breaks. > [...] > String objects for literals are already created in the compile time, which is far > before you redefine the initialize method.   [...] This should not matter. In context of irb this becomes more clear (redefinition of the initialize method happens clearly before it's usage.). #String2.rb class String def initialize(val) self.replace(val) puts object_id end def my_method_test print self.inspect, " test\n" end end # command line $ irb irb(main):001:0> original = String.new("original") => "original" irb(main):002:0> load "String2.rb" => true irb(main):003:0> altered = String.new("altered") 21878604 => "altered" irb(main):004:0> altered.my_method_test => "has method " irb(main):005:0> literal = "literal" => "literal" irb(main):006:0> literal.my_method_test => "has method " irb(main):007:0> - The initialize method is an integral part of the class String. From the moment that "String2.rb" is loaded, the initialize method of class String has been validly redefined. (The behaviour of the String class within the "irb session" is altered) The altered initialize method is now an integral part of the class String. The altered String object behaves as expected (responds to "my_method_test, initialized via redefined initialize method). The String(Literal) object responds to "my_method_test", but it is was not initialized with the redefined initialize method. - The "Literal Instantiation" calls the original (core-C-level) String initialize method instead of the redefined one (user-language-level). This *breaks* the object model. . -- http://lazaridis.com