0
- def Continuation.create(*args, &block) #:nodoc:
0
- cc = nil; result = callcc {|c| cc = c; block.call(cc) if block and args.empty?}
0
+ class Continuation #:nodoc:
0
+ def create(*args, &block)
0
+ cc = nil; result = callcc {|c| cc = c; block.call(cc) if block and args.empty?}
0
-# This method returns the binding of the method that called your
0
-# method. It will raise an Exception when you're not inside a method.
0
-# def inc_counter(amount = 1)
0
-# Binding.of_caller do |binding|
0
-# # Create a lambda that will increase the variable 'counter'
0
-# # in the caller of this method when called.
0
-# inc = eval("lambda { |arg| counter += arg }", binding)
0
-# # We can refer to amount from inside this block safely.
0
-# # No other statements can go here. Put them inside the block.
0
-# 2.times { inc_counter }
0
-# Binding.of_caller must be the last statement in the method.
0
-# This means that you will have to put everything you want to
0
-# do after the call to Binding.of_caller into the block of it.
0
-# This should be no problem however, because Ruby has closures.
0
-# If you don't do this an Exception will be raised. Because of
0
-# the way that Binding.of_caller is implemented it has to be
0
-def Binding.of_caller(&block) #:nodoc:
0
- old_critical = Thread.critical
0
- Thread.critical = true
0
- cc, result, error, extra_data = Continuation.create(nil, nil)
0
+ # This method returns the binding of the method that called your
0
+ # method. It will raise an Exception when you're not inside a method.
0
+ # It's used like this:
0
+ # def inc_counter(amount = 1)
0
+ # Binding.of_caller do |binding|
0
+ # # Create a lambda that will increase the variable 'counter'
0
+ # # in the caller of this method when called.
0
+ # inc = eval("lambda { |arg| counter += arg }", binding)
0
+ # # We can refer to amount from inside this block safely.
0
+ # # No other statements can go here. Put them inside the block.
0
+ # 2.times { inc_counter }
0
+ # Binding.of_caller must be the last statement in the method.
0
+ # This means that you will have to put everything you want to
0
+ # do after the call to Binding.of_caller into the block of it.
0
+ # This should be no problem however, because Ruby has closures.
0
+ # If you don't do this an Exception will be raised. Because of
0
+ # the way that Binding.of_caller is implemented it has to be
0
+ old_critical = Thread.critical
0
+ Thread.critical = true
0
+ cc, result, error, extra_data = Continuation.create(nil, nil)
0
- tracer = lambda do |*args|
0
- type, context, extra_data = args[0], args[4], args
0
- # First this method and then calling one will return --
0
- # the trace event of the second event gets the context
0
- # of the method which called the method that called this
0
- # It would be nice if we could restore the trace_func
0
- # that was set before we swapped in our own one, but
0
- # this is impossible without overloading set_trace_func
0
+ tracer = lambda do |*args|
0
+ type, context, extra_data = args[0], args[4], args
0
+ # First this method and then calling one will return --
0
+ # the trace event of the second event gets the context
0
+ # of the method which called the method that called this
0
+ # It would be nice if we could restore the trace_func
0
+ # that was set before we swapped in our own one, but
0
+ # this is impossible without overloading set_trace_func
0
+ cc.call(eval("binding", context), nil, extra_data)
0
+ elsif type == "line" then
0
+ elsif type == "c-return" and extra_data[3] == :set_trace_func then
0
- cc.call(eval("binding", context), nil, extra_data)
0
+ error_msg = "Binding.of_caller used in non-method context or " +
0
+ "trailing statements of method using it aren't in the block."
0
+ cc.call(nil, lambda { raise(ArgumentError, error_msg) }, nil)
0
- elsif type == "line" then
0
- elsif type == "c-return" and extra_data[3] == :set_trace_func then
0
- error_msg = "Binding.of_caller used in non-method context or " +
0
- "trailing statements of method using it aren't in the block."
0
- cc.call(nil, lambda { raise(ArgumentError, error_msg) }, nil)
0
- set_trace_func(tracer)
0
- Thread.critical = old_critical
0
- when 1 then yield(result)
0
- else yield(result, extra_data)
0
+ set_trace_func(tracer)
0
+ Thread.critical = old_critical
0
+ when 1 then yield(result)
0
+ else yield(result, extra_data)
0
\ No newline at end of file
Comments
No one has commented yet.