# Catching attr\_accessor with set\_trace\_func

**URL:** <https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137>\
**Category:** Ruby\
**Created:** [November 16, 2006, 5:52am UTC](https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137 "2006-11-16T05:52:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wilson\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@Wilson\_B](https://www.ruby-forum.com/u/Wilson_B)\
**Post date:** [November 16, 2006, 5:52am UTC](https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137/1 "2006-11-16T05:52:41Z")

</div>

Code using attr\_accessor:

class User  
attr\_accessor :something  
def initialize  
@something = 0  
end  
end

@user = User.new

set\_trace\_func(lambda {|\*trace| STDERR.puts trace.inspect})  
@user.something

#########  
prints this to STDERR:  
[“line”, “trace\_func.rb”, 11, nil, #Binding:0x2946df0, false]  
#########

Functionally equivalent code with a hand-written method, instead of  
attr\_accessor:

class User  
def something  
@something  
end  
def initialize  
@something = 0  
end  
end

@user = User.new

set\_trace\_func(lambda {|\*trace| STDERR.puts trace.inspect})  
@user.something

#########  
prints this to STDERR:  
[“line”, “trace\_func.rb”, 13, nil, #Binding:0x2946d78, false]  
[“call”, “trace\_func.rb”, 2, :something, #Binding:0x2946c10, User]  
[“line”, “trace\_func.rb”, 3, :something, #Binding:0x2946bc8, User]  
[“return”, “trace\_func.rb”, 4, :something, #Binding:0x2946aa8, User]  
#########

How can I capture the fact that the ‘something’ accessor was called in  
the first example?

---

<div class="post-metadata">

**Author:** ![Wilson\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@Wilson\_B](https://www.ruby-forum.com/u/Wilson_B)\
**Post date:** [November 16, 2006, 8:52am UTC](https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137/2 "2006-11-16T08:52:55Z")

</div>

Hi,

At Thu, 16 Nov 2006 13:51:53 +0900,  
Wilson B. wrote in [ruby-talk:225245]:

> How can I capture the fact that the ‘something’ accessor was called in  
> the first example?

You can’t now.

Maybe, new event types might be better.

---

<div class="post-metadata">

**Author:** ![Wilson\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@Wilson\_B](https://www.ruby-forum.com/u/Wilson_B)\
**Post date:** [November 16, 2006, 9:34am UTC](https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137/3 "2006-11-16T09:34:59Z")

</div>

Hi,

At Thu, 16 Nov 2006 16:52:07 +0900,  
Nobuyoshi N. wrote in [ruby-talk:225257]:

> — eval.c 7 Nov 2006 09:38:12 -0000 1.958  
> +++ eval.c 16 Nov 2006 07:32:05 -0000

Sorry, it was wrong.

---

<div class="post-metadata">

**Author:** ![Wilson\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@Wilson\_B](https://www.ruby-forum.com/u/Wilson_B)\
**Post date:** [November 16, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137/4 "2006-11-16T16:02:14Z")

</div>

On 11/16/06, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> At Thu, 16 Nov 2006 16:52:07 +0900,  
> Nobuyoshi N. wrote in [ruby-talk:225257]:
> 
> > — eval.c 7 Nov 2006 09:38:12 -0000 1.958  
> > +++ eval.c 16 Nov 2006 07:32:05 -0000
> 
> Sorry, it was wrong.

Thank you. That explains why it wasn’t traceable.

While I have you here, perhaps you have an idea about the problem I am  
trying to solve.

Given:  
@user.something.trace\_me

…I am trying to ‘unwind’ the calling order, so that the ‘trace\_me’  
method can get a reference to @user and :something.  
I need to do this in a general way, where the name of the object  
(@user), and the last message it was sent (:something) are not known  
in advance.

Is set\_trace\_func the only way to do that? Kernel#caller doesn’t seem  
like enough.

---

<div class="post-metadata">

**Author:** ![Wilson\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@Wilson\_B](https://www.ruby-forum.com/u/Wilson_B)\
**Post date:** [November 16, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137/5 "2006-11-16T16:26:23Z")

</div>

Hi,

At Fri, 17 Nov 2006 00:01:43 +0900,  
Wilson B. wrote in [ruby-talk:225298]:

> Given:  
> @user.something.trace\_me
> 
> …I am trying to ‘unwind’ the calling order, so that the ‘trace\_me’  
> method can get a reference to @user and :something.  
> I need to do this in a general way, where the name of the object  
> (@user), and the last message it was sent (:something) are not known  
> in advance.

What do you expect when the receiver isn’t assigned to any  
variable?

---

<div class="post-metadata">

**Author:** ![Wilson\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@Wilson\_B](https://www.ruby-forum.com/u/Wilson_B)\
**Post date:** [November 16, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/catching-attr-accessor-with-set-trace-func/76137/6 "2006-11-16T16:38:27Z")

</div>

On 11/16/06, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > (@user), and the last message it was sent (:something) are not known  
> > in advance.
> 
> What do you expect when the receiver isn’t assigned to any  
> variable?

Something indicating that would be fine. I don’t need a handle on  
‘intermediate’ objects, just ones that have a real lexical existence  
in source code.
