public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Tagged the 0.9.4 release

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@444 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
dhh (author)
Sun Jan 16 18:30:07 -0800 2005
commit  1e3b5b1fcb3380603e7bb60f7dfc4e7a504e4055
tree    802fb875c38fa32552ce9fbab7b675b9b91c365b
parent  e98a14012d7b2102858057c8ffa227ff419c057c
...
1
2
3
4
5
6
7
 
 
 
 
 
 
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
57
 
 
 
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
 
 
 
 
 
 
 
 
79
80
81
 
82
...
1
2
3
 
 
 
 
4
5
6
7
8
9
10
11
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
 
 
 
 
 
 
 
 
 
 
 
 
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
68
69
70
71
 
 
 
 
 
 
 
 
 
72
 
73
 
 
 
 
 
 
 
 
74
75
76
77
78
79
80
81
82
83
84
 
85
86
0
@@ -1,81 +1,85 @@
0
 begin
0
   require 'simplecc'
0
 rescue LoadError
0
- def Continuation.create(*args, &block) #:nodoc:
0
- cc = nil; result = callcc {|c| cc = c; block.call(cc) if block and args.empty?}
0
- result ||= args
0
- return *[cc, *result]
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
+ result ||= args
0
+ return *[cc, *result]
0
+ end
0
   end
0
 end
0
 
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
-#
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
-# inc.call(amount)
0
-# end
0
-# # No other statements can go here. Put them inside the block.
0
-# end
0
-# counter = 0
0
-# 2.times { inc_counter }
0
-# counter # => 2
0
-#
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
-# done this way.
0
-def Binding.of_caller(&block) #:nodoc:
0
- old_critical = Thread.critical
0
- Thread.critical = true
0
- count = 0
0
- cc, result, error, extra_data = Continuation.create(nil, nil)
0
- error.call if error
0
+class Binding #:nodoc:
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
+ #
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
+ # inc.call(amount)
0
+ # end
0
+ # # No other statements can go here. Put them inside the block.
0
+ # end
0
+ # counter = 0
0
+ # 2.times { inc_counter }
0
+ # counter # => 2
0
+ #
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
+ # done this way.
0
+ def of_caller(&block)
0
+ old_critical = Thread.critical
0
+ Thread.critical = true
0
+ count = 0
0
+ cc, result, error, extra_data = Continuation.create(nil, nil)
0
+ error.call if error
0
 
0
- tracer = lambda do |*args|
0
- type, context, extra_data = args[0], args[4], args
0
- if type == "return"
0
- count += 1
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
- # method.
0
- if count == 2
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
- # in current Ruby.
0
+ tracer = lambda do |*args|
0
+ type, context, extra_data = args[0], args[4], args
0
+ if type == "return"
0
+ count += 1
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
+ # method.
0
+ if count == 2
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
+ # in current Ruby.
0
+ set_trace_func(nil)
0
+ cc.call(eval("binding", context), nil, extra_data)
0
+ end
0
+ elsif type == "line" then
0
+ nil
0
+ elsif type == "c-return" and extra_data[3] == :set_trace_func then
0
+ nil
0
+ else
0
         set_trace_func(nil)
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
       end
0
- elsif type == "line" then
0
- nil
0
- elsif type == "c-return" and extra_data[3] == :set_trace_func then
0
- nil
0
- else
0
- set_trace_func(nil)
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
     end
0
- end
0
 
0
- unless result
0
- set_trace_func(tracer)
0
- return nil
0
- else
0
- Thread.critical = old_critical
0
- case block.arity
0
- when 1 then yield(result)
0
- else yield(result, extra_data)
0
+ unless result
0
+ set_trace_func(tracer)
0
+ return nil
0
+ else
0
+ Thread.critical = old_critical
0
+ case block.arity
0
+ when 1 then yield(result)
0
+ else yield(result, extra_data)
0
+ end
0
     end
0
   end
0
-end
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.