[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Observe variable set and variable change

Saladin Mundi

11/17/2007 9:28:00 AM

Hey folks,

is there any kind of Ruby / C function with which it is possible to see =
when a variable is set or changed?

Something like:

class x

def y()
z =3D 42
z =3D 100
end
end

So that a function takes care about setting the 42 and another for =
caring about changing the value of z to 100?

thanks in advance

sala

6 Answers

Tim Hunter

11/17/2007 2:09:00 PM

0

saladin.mundi@gmx.de wrote:
> Hey folks,
>
> is there any kind of Ruby / C function with which it is possible to see when a variable is set or changed?
>
> Something like:
>
> class x
>
> def y()
> z = 42
> z = 100
> end
> end
>
> So that a function takes care about setting the 42 and another for caring about changing the value of z to 100?
>
> thanks in advance
>
> sala
>


--
RMagick: http://rmagick.ruby...

Tim Hunter

11/17/2007 2:11:00 PM

0

saladin.mundi@gmx.de wrote:
> Hey folks,
>
> is there any kind of Ruby / C function with which it is possible to see when a variable is set or changed?
>
> Something like:
>
> class x
>
> def y()
> z = 42
> z = 100
> end
> end
>
> So that a function takes care about setting the 42 and another for caring about changing the value of z to 100?
>
> thanks in advance
>
> sala
>

Let's try again...

There's Observable:

~$ ri Observable
------------------------------------------------------ Class: Observable
Implements the Observable design pattern as a mixin so that other
objects can be notified of changes in state. See observer.rb for
details and an example.

------------------------------------------------------------------------

Instance methods:
add_observer, changed, changed?, count_observers, delete_observer,
delete_observers, notify_observers
~$


--
RMagick: http://rmagick.ruby...

Marc Heiler

11/17/2007 3:54:00 PM

0

> is there any kind of Ruby / C function with which it is possible to see
> when a variable is set or changed?

You can try trace_var (or whatever the name was)

trace_var :$x, proc{print "$x is now ", $x, "\n"}
--
Posted via http://www.ruby-....

Saladin Mundi

11/17/2007 4:11:00 PM

0

> You can try trace_var (or whatever the name was)
>
> trace_var :$x, proc{print "$x is now ", $x, "\n"}

thanks guys. to trace_var
this as i see is just for tracing global vars. it would be great if there is
a way for also tracing vars inside methods.

def x()
z = 10
z = 42
end

--> the variable z was initialized with the value 10
--> the variable z was changed with the value 42


Wolfgang Nádasi-donner

11/17/2007 5:57:00 PM

0

unknown wrote:
> a way for also tracing vars inside methods.
>
> def x()
> z = 10
> z = 42
> end

I don't think that there exists some method for this purpose. You can
use Ruby's trace possibilities or a simple method for looking at local
variables at special points...

def show_local_vars(bind, *d)
puts '----- variables -----'
vars = eval("local_variables", bind)
(d.length == 0 ? vars : (d.map{|s|s.to_s} & vars)).each do |v|
begin
puts "#{v}=#{eval(v,bind)}"
rescue
puts "+++++ Variable '#{v}' not defined"
end
end
end

def x()
z = 10
show_local_vars(binding)
z = 42
show_local_vars(binding, :z, :a)
end

x

...outputs...

----- variables -----
z=10
----- variables -----
z=42

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....

Saladin Mundi

11/19/2007 2:33:00 PM

0

> I don't think that there exists some method for this purpose. You can
> use Ruby's trace possibilities or a simple method for looking at local
> variables at special points...
>

What methods does ruby itself [so, the C-core] uses to initialize a variable
(local, global, instance, whatever)?
And what method(s) is(are) used to give an exisiting variable a new value?

thanks