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

···

--
Posted via http://www.ruby-forum.com/.

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 Jun 13, 2008, at 6:24 PM, Mehdi Adda wrote:

Tobias Weber wrote:

In article <d393d2832e1f94df7d765538b9973fe4@ruby-forum.com>,

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

···

Mehdi Adda <mehdi.adda@gmail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Fri, Jun 13, 2008 at 10:04 PM, fedzor <fedzor@gmail.com> wrote:

a = 5
a.trace_var {|new| puts "a was changed to '#{new}'" }

--
Avdi

Home: http://avdi.org
Developer Blog: Avdi Grimm, Code Cleric
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

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

Regards,
Florian Gilcher

···

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

Tobias Weber wrote:

In article <d393d2832e1f94df7d765538b9973fe4@ruby-forum.com>,
Mehdi Adda <mehdi.adda@gmail.com> 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

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

···

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

On Fri, Jun 13, 2008 at 10:04 PM, fedzor <fedzor@gmail.com> 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| ...}