[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

fire event when the value of a variable is changed

Mehdi Adda

6/13/2008 10:24:00 PM

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

7 Answers

fedzor

6/14/2008 2:05:00 AM

0


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



Avdi Grimm

6/14/2008 3:36:00 AM

0

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

And it only works for globals, not local variables.


--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Tobias Weber

6/14/2008 7:12:00 AM

0

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.

--
Tobias Weber

Mehdi Adda

6/14/2008 1:08:00 PM

0

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
--
Posted via http://www.ruby-....

Phlip

6/14/2008 1:50:00 PM

0

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

Exactly why can't you refactor the variable into an instance variable, then
override its def variable= method?

If it's a local variable, it should be inside a method that's only 1 to 5
lines long, so detecting when it changes should be trivial, right?


Florian Gilcher

6/14/2008 2:05:00 PM

0

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


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

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

fedzor

6/14/2008 4:36:00 PM

0


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

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