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.
···
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
------------------------------------------------------------------------
Instance methods:
add_observer, changed, changed?, count_observers, delete_observer,
delete_observers, notify_observers
~$
--
RMagick: http://rmagick.rubyforge.org/
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-forum.com/\.
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
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-forum.com/\.
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