Observe variable set and variable change

S

saladin.mundi

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
 
T

Tim Hunter

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
 
T

Tim Hunter

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
~$
 
M

Marc Heiler

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"}
 
S

saladin.mundi

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
 
W

Wolfgang Nádasi-Donner

unknown said:
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
 
S

saladin.mundi

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top