From: Trans Date: 2009-01-12T04:13:29+09:00 Subject: Re: Binding.of_caller examples don't work. On Jan 11, 1:11 pm, "Pit Capitain" wrote: > 2009/1/11 Phlip : > > > Trans wrote: > >> Binding.of_caller is no longer possible. > > > Is a workaround available? > > It seems that the original idea of Florian Groß is still valid. Here's > a version working for my Ruby 1.8.6 on Windows: > >   ## >   #  Re-implementation of Binding.of_caller for Ruby 1.8.6 >   #  (C) 2009 Pit Capitain >   #  Original idea and implementation by Florian Gross >   # >   def Binding.of_caller > >     old_critical = Thread.critical >     Thread.critical = true > >     restart_cc = result = error = nil > >     expected_events = %w"line c-call c-return return return" >     tracer = lambda do |event, file, line, id, binding, classname| >       if expected_events.empty? >         set_trace_func(nil) >         result = binding >         restart_cc.call >       elsif event != expected_events.shift >         set_trace_func(nil) >         error = >           "Binding.of_caller used in non-method context" + >           " or trailing statements of method using it aren't in the block." >         restart_cc.call >       end >     end > >     callcc { |cc| restart_cc = cc } > >     if result >       yield(result) >     elsif error >       raise(ArgumentError, error) >     else >       set_trace_func(tracer) >     end > >   ensure >     Thread.critical = old_critical >   end Cool. But word or warning to Doug, I would not recommend using #set_trace_func in a production application. It can slow things down considerably. T.