# Fire event when the value of a variable is changed

**URL:** <https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440>\
**Category:** Ruby\
**Created:** [June 14, 2008, 12:24am UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440 "2008-06-14T00:24:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [June 14, 2008, 12:24am UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440/1 "2008-06-14T00:24:58Z")

</div>

hi !

what is the best way to execute a given method each time the value of a  
given variable is changed.

I have told that if the value of the variable is changed via a sitter,  
it’s possible to use a method chaining. How if there is no setter ?

Thanks in advance,

Addame

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [June 14, 2008, 4:06am UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440/2 "2008-06-14T04:06:25Z")

</div>

On Jun 13, 2008, at 6:24 PM, Mehdi A. wrote:

> hi !  
> hej!

> what is the best way to execute a given method each time the value  
> of a  
> given variable is changed.

sweetsauce. this is a pretty underused trick in ruby:

a = 5  
a.trace\_var {|new| puts “a was changed to ‘#{new}’” }

huzzah!

-------------------------------------------------------|  
~ Ari  
seydar: it’s like a crazy love triangle of Kernel commands and C code

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [June 14, 2008, 5:37am UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440/3 "2008-06-14T05:37:37Z")

</div>

On Fri, Jun 13, 2008 at 10:04 PM, fedzor [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a = 5  
> a.trace\_var {|new| puts “a was changed to ‘#{new}’” }

Did you actually try this? And if so, on what version of Ruby?

According to RI and the results of my own experimentation, trace\_var  
is called like so:

trace\_var(:$a) {|value| …}

And it only works for globals, not local variables.

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [June 14, 2008, 9:15am UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440/4 "2008-06-14T09:15:47Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
Mehdi A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have told that if the value of the variable is changed via a sitter,  
> it’s possible to use a method chaining. How if there is no setter ?

What kind of variable? Global, local, instance? For the latter you could  
write your own attr\_accessor method that either creates a call to a  
custom method or uses update() of Observer.

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [June 14, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440/5 "2008-06-14T16:06:41Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jun 14, 2008, at 3:07 PM, Mehdi A. wrote:

> > write your own attr\_accessor method that either creates a call to a
> 
> Addame

Even if you change the setter, you won’t be able to trace direct  
assignment.

Regards,  
Florian G.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkhT0DsACgkQJA/zY0IIRZZtMQCcDXUu3UKxEiOi9ixEW3J5LUKr  
TaoAoIXU3Dom1cDnzGCoOUmRI0Lt/XDl  
=SYa5  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [June 14, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440/6 "2008-06-14T15:08:38Z")

</div>

Tobias W. wrote:

> In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
> Mehdi A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have told that if the value of the variable is changed via a sitter,  
> > it’s possible to use a method chaining. How if there is no setter ?
> 
> What kind of variable? Global, local, instance? For the latter you could  
> write your own attr\_accessor method that either creates a call to a  
> custom method or uses update() of Observer.

Thanks for the answer … if fact when it’s an instance variable, it’s  
possible to (re)define the setter… the problem is when the variable is  
local.

I checked the use of trace\_var, and as Avdi said, it’s only used to  
trace global variables assignments.

Addame

---

<div class="post-metadata">

**Author:** ![addame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecd19e/32.png) [@addame](https://www.ruby-forum.com/u/addame)\
**Post date:** [June 14, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/fire-event-when-the-value-of-a-variable-is-changed/140440/7 "2008-06-14T18:37:06Z")

</div>

On Jun 13, 2008, at 11:36 PM, Avdi G. wrote:

> On Fri, Jun 13, 2008 at 10:04 PM, fedzor [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > a = 5  
> > a.trace\_var {|new| puts “a was changed to ‘#{new}’” }
> 
> Did you actually try this? And if so, on what version of Ruby?
> 
> According to RI and the results of my own experimentation, trace\_var  
> is called like so:
> 
> trace\_var(:$a) {|value| …}

I tried it a while ago… but not _Exactly_ that line of code… sorry!

-------------------------------------------------------|  
~ Ari  
seydar: it’s like a crazy love triangle of Kernel commands and C code
