fire event when the value of a variable is changed

M

Mehdi Adda

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
 
F

fedzor

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
 
T

Tobias Weber

Mehdi Adda said:
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.
 
M

Mehdi Adda

Tobias said:
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
 
F

Florian Gilcher

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


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

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

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

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

fedzor

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top