From: Alex Chaffee Date: 2012-07-06T13:00:11+09:00 Subject: Re: Using binding + set trace func to capture execution state Nice timing! For kicks, I just wrote a gem that emulates Java's behavior of printing stack traces when you hit ctrl-backslash. (The idea is to debug programs that are stuck in an infinite loop or something somewhere.) I tried to also print local and instance variables but hit a wall; this may help with that. It's not really ready for prime time but you can check it out: http://github.com/alexch/thready - A On Wed, Jul 4, 2012 at 1:49 AM, Robert Klemme wrote: > On Wed, Jul 4, 2012 at 9:57 AM, Bartosz DziewoƄski wrote: >> You can use the `local_variables` method to get a list of local >> variables in current scope. So maybe something like this: >> >> $values_at_time = {} >> >> set_trace_func proc { |event, file, line, id, binding, classname| >> if event == "line" >> $values_at_time[line] = binding.eval('local_variables.map{|v| [v, >> eval(v.to_s)] }') >> end >> } >> >> a = 5 >> stuff = 'asd' >> a += 8 >> >> set_trace_func nil >> pp $values_at_time > > That still suffers from the issue of changes in referenced object > which will modify the stored state. Even adding to an Array will > change history. As I said earlier you would need to store a complete > object graph, something like > > $values_at_time = {} > > set_trace_func proc { |event, file, line, id, binding, classname| > if event == "line" > $values_at_time[line] = > Marshal.dump(binding.eval("local_variables").inject({}) > {|h,v|h[v]=binding.eval(v.to_s);h}) > end > } > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > -- Alex Chaffee - alex@stinky.com http://alexchaffee.com http://twitter.com/alexch