From: Dave Thomas Date: 2001-09-24T21:27:35+09:00 Subject: [ruby-talk:21629] Re: Please translate this easy snip of C++ to Ruby Carlos writes: > Yes, I think easier. Based on what you said about how it is done in Python > and on Dave's solution, here is a simple, poorly tested approach: > > --- > def trace_(&b) > file,line_nr=caller()[0].split(':') > line_nr=line_nr.to_i > linecnt=1 > expr=nil > File.open(file).each_line do |line| > if linecnt==line_nr > if line =~ /(^|\W)trace_\s*{\s*(.*?)\s*}/ > expr = $2 > end > break > end > linecnt+=1 > end > if expr > puts "#{expr}: #{eval(expr, b).inspect}" > end > end Why not just 'yield' to get the value of the block in this case? I don't think the 'eval' is needed. Dave