From: "Anders Engström" Date: 2004-03-19T04:44:31+09:00 Subject: Class.allocate callback Hi. I want to get a callback whenever an object is created in the vm. To do this I have overridden 'Class.new', and it works as long as objects are created through Object.new. But - I also want to get callbacks when objects are created during unmarshalling of serialized data.. I thought that I could override Class.allocate to get a callback when this happens - but I never get the callback. Is this a bug? I've attached a simple script that illustrates my problem. Run the script as "ruby load.rb xxx" two times in a row to see the issue. # # load.rb # # class Class alias_method :old_new, :new alias_method :old_allocate, :allocate def new(*args) instance = old_new(*args) puts "Created instance #{instance}" return instance end def allocate instance = old_allocate puts "Allocated instance #{instance}" return instance end end class Dummy attr_reader :id, :name, :tstamp attr_writer :name def initialize(id, name=nil) @id = id @name = name @tstamp = Time.now end def touch @tstamp = Time.now end end d = nil if ARGV[0] if File.exist?("load.ser") puts "..loading from serialized file..." File.open("load.ser"){|f| d = Marshal.load(f) } end end unless d puts "....creating new instance...." d = Dummy.new(44, "Hello") end puts d.inspect d.touch d.name = "xxxxx" puts d.inspect if ARGV[0] puts "...saving to serialized file..." File.open("load.ser", "w+"){|f| Marshal.dump(d, f) } end -- /** * Anders Engstr�m, aengstrom@gnejs.net * ------------------------------------- * Your mind is like an umbrella. * It doesn't work unless you open it. * /Frank Zappa */