On 9 Ιούν, 21:39, Ilias Lazaridis <il... / lazaridis.com> wrote:
> class String
> alias_method :orig_initialize, :initialize
> def initialize(val)
> orig_initialize "OBSERVED: " + val
> end
> def my_method_test
> print self.inspect, " test\n"
> end
> end
>
> oo_string = String.new("The OO String")
> li_string = "The Literal String"
>
> print "Class: ", oo_string.class, " - content: ", oo_string, "\n"
> print "Class: ", li_string.class, " - content: ", li_string, "\n"
> oo_string.my_method_test
> li_string.my_method_test
>
> #OUTPUT
> #=> Class: String - content: OBSERVED: The OO String
> #=> Class: String - content: The Literal String
> #=> "OBSERVED: The OO String" test
> #=> "The Literal String" test
>
> -
>
> 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.
>
> The statement "Everything is an Object" becomes invalid, because e.g.
> a string object instantiated from a literal behaves differently that a
> string object instantiated normally via new() (although they share the
> same class, and thus should behave the same).
>
> My understanding is, that this is a know-issue and a trade-off due to
> performance issues.
>
> The questions are:
>
> b) Is there any way to track (intercept) the instantiation of objects
> (especially those instantiated from literals)
> 1) without a C-level extension
> 2) with a C-level extension
>
> The interception can be post instantiation.
>
> Underlying Requirement:
>
> Ability to track instantiation of every object within the system.

Related Issue:

Literal Instantiation breaks Object Model
http://redmine.ruby-lang.org/issues/4893

Related Issue (Attempt to workaround)

Provide Class#cb_object_instantiated_from_literal(object)
http://redmine.ruby-lang.org/issues/4845

.

--
http://lazaridis.com