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
*/
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?
[snip script]
2 fast 2 furious… I forgot one crucial piece of information:
ruby 1.8.1 (2004-02-03) [i386-linux]
//Anders
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?
Not really a bug. Hooking every instance allocation might too slow
for the current implementation. Sometimes I have to choose
performance over flexibility.
Thanks for the reply. I can understand this decision - it makes sense.
It would be nice if this behaviour was documented though
Coming from the Java world I would say that Ruby is flexible enough,
even without callback-on-allocate. And - coming from the Java world it’s
absolutely wonderful to ask a question in a ng and get a reply from the
actual author of the language in < 24 hours
(I wonder if James Gosling frequents comp.lang.java…
//Anders
···
In message “Class.allocate callback” > on 04/03/19, Anders Engström aengstrom@gnejs.net writes:
–
/**
- Anders Engström, aengstrom@gnejs.net
-
- Your mind is like an umbrella.
- It doesn’t work unless you open it.
- /Frank Zappa
*/