Fire event when the value of a variable is changed

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

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

On Fri, Jun 13, 2008 at 10:04 PM, fedzor [email protected] 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
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

In article [email protected],
Mehdi A. [email protected] 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.

-----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-----

Tobias W. wrote:

In article [email protected],
Mehdi A. [email protected] 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

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

On Fri, Jun 13, 2008 at 10:04 PM, fedzor [email protected] 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