[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

introspection question

jaydonnell

9/6/2006 12:01:00 AM

is there a way for an object to tell you where it was created a la
stacktraces?

I tried to hack it in myself, but it's really slowwwwwww. Here was my
hack attempt. I also wanted each object to tell me the time it was
created which works fine.

class Object
attr_accessor :mp_created_at
attr_accessor :origin_trace
end

class Class
alias oldNew new
def new(*args, &block)
obj = oldNew(*args, &block)
begin
raise "force a stack trace"
rescue Exception => err
obj.origin_trace = err.backtrace
end
#a hack that I need for a reason which I haven't taken the time to
figure out
obj.mp_created_at = Time.now() unless obj.frozen?
return obj
end
end

4 Answers

Logan Capaldo

9/6/2006 12:55:00 AM

0


On Sep 5, 2006, at 8:05 PM, jdonnell wrote:

> is there a way for an object to tell you where it was created a la
> stacktraces?
>
> I tried to hack it in myself, but it's really slowwwwwww. Here was my
> hack attempt. I also wanted each object to tell me the time it was
> created which works fine.
>
> class Object
> attr_accessor :mp_created_at
> attr_accessor :origin_trace
> end
>

Is this close enough?

class Class
alias rb_new new
def new(*args, &block)
stack = caller
obj = rb_new(*args, &block)
obj.origin_trace = stack
obj.mp_created_at = Time.now unless obj.frozen?
obj
end
end




Robert Klemme

9/6/2006 8:56:00 AM

0

On 06.09.2006 02:00, jdonnell wrote:
> is there a way for an object to tell you where it was created a la
> stacktraces?
>
> I tried to hack it in myself, but it's really slowwwwwww. Here was my
> hack attempt. I also wanted each object to tell me the time it was
> created which works fine.
>
> class Object
> attr_accessor :mp_created_at
> attr_accessor :origin_trace
> end
>
> class Class
> alias oldNew new
> def new(*args, &block)
> obj = oldNew(*args, &block)
> begin
> raise "force a stack trace"
> rescue Exception => err
> obj.origin_trace = err.backtrace
> end
> #a hack that I need for a reason which I haven't taken the time to
> figure out

Probably because you were not aware of #caller:
http://www.ruby-doc.org/core/classes/Kernel.ht...

>> def foo() caller 0 end
=> nil
>> foo
=> ["(irb):6:in `foo'", "(irb):7:in `irb_binding'",
"/usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'", ":0"]

> obj.mp_created_at = Time.now() unless obj.frozen?
> return obj
> end
> end

Kind regards

robert

jaydonnell

9/14/2006 12:14:00 AM

0

Thanks :) That's exactly what i was looking for.



Logan Capaldo wrote:
> On Sep 5, 2006, at 8:05 PM, jdonnell wrote:
>
> > is there a way for an object to tell you where it was created a la
> > stacktraces?
> >
> > I tried to hack it in myself, but it's really slowwwwwww. Here was my
> > hack attempt. I also wanted each object to tell me the time it was
> > created which works fine.
> >
> > class Object
> > attr_accessor :mp_created_at
> > attr_accessor :origin_trace
> > end
> >
>
> Is this close enough?
>
> class Class
> alias rb_new new
> def new(*args, &block)
> stack = caller
> obj = rb_new(*args, &block)
> obj.origin_trace = stack
> obj.mp_created_at = Time.now unless obj.frozen?
> obj
> end
> end

James A. Donald

9/23/2009 4:06:00 AM

0

On Wed, 23 Sep 2009 11:57:12 +1000, James A. Donald
> A coup d'etat involves elements of the military.

(Though secret police coups, palace guard coups, etc can also occur.
Secret police might well be more subject to foreign infiltration than
the military)

--
----------------------
We have the right to defend ourselves and our property, because
of the kind of animals that we are. True law derives from this
right, not from the arbitrary power of the omnipotent state.

http://ww...