From: Florian Gross Date: 2004-09-30T03:35:05+09:00 Subject: Re: Binding of the Caller (redux) Gavin Kistner wrote: > On Sep 28, 2004, at 10:29 AM, Florian Gross wrote: >> Gavin Kistner wrote: >>> So does "... trailing statements of method using it aren't in the >>> block" mean "you can't place any statements after the >>> Binding.of_caller invocation"? (Like it irrevocably changes the >>> context and can't get you back to where you were or something?) >> I tried to make the interface obvious in the documentation (see the >> sample), but I seem to have failed. Do you have any suggestions for >> how I could fix this? > Er, to which sample are you referring? The old one, but it might have been a bit unclear. I have changed the documentation according to your suggestions, thank you. Here's the new documentation: # This method returns the binding of the method that called your # method. It will raise an Exception when you're not inside a method. # # It's used like this: # def inc_counter(amount = 1) # Binding.of_caller do |binding| # # Create a lambda that will increase the variable 'counter' # # in the caller of this method when called. # inc = eval("lambda { |arg| counter += arg }", binding) # # We can refer to amount from inside this block safely. # inc.call(amount) # end # # No other statements can go here. Put them inside the block. # end # counter = 0 # inc_counter(2) # counter # => 2 # # Binding.of_caller must be the last statement in the method. # This means that you will have to put everything you want to # do after the call to Binding.of_caller into the block of it. # This should be no problem however, because Ruby has closures. # If you don't do this an Exception will be raised. Because of # the way that Binding.of_caller is implemented it has to be # done this way. > - Gavin, who looks forward to a version of Ruby which has a simple > "#caller" method that points to the instance which invoked the current > method. Yup, I also think that all this ought to be easier in a future Ruby. This is the best I can do for now, unfortunately. Regards, Florian Gross