-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10.rb
138 lines (117 loc) · 3.16 KB
/
10.rb
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module ClassTrace
# This array holds our list of files loaded and classes defined.
# Each element is a subarray holding the class defined or the
# file loaded and the stack frame where it was defined or loaded.
T = [] # Array to hold the files loaded
# Now define the constant OUT to specify where tracing output goes.
# This defaults to STDERR, but can also come from command-line arguments
if x = ARGV.index("--traceout")
# If argument exists
OUT = File.open(ARGV[x+1], "w") # Open the specified file
ARGV[x,2] = nil
# And remove the arguments
else
OUT = STDERR
# Otherwise default to STDERR
end
end
# Alias chaining step 1: define aliases for the original methods
alias original_require require
alias original_load load
# Alias chaining step 2: define new versions of the methods
def require(file)
ClassTrace::T << [file,caller[0]]
# Remember what was loaded where
original_require(file)
# Invoke the original method
end
def load(*args)
ClassTrace::T << [args[0],caller[0]] # Remember what was loaded where
original_load(*args)
# Invoke the original method
end
# This hook method is invoked each time a new class is defined
def Object.inherited(c)
ClassTrace::T << [c,caller[0]]
# Remember what was defined where
end
# Kernel.at_exit registers a block to be run when the program exits
# We use it to report the file and class data we collected
at_exit {
o = ClassTrace::OUT
o.puts "="*60
o.puts "Files Loaded and Classes Defined:"
o.puts "="*60
ClassTrace::T.each do |what,where|
if what.is_a? Class # Report class (with hierarchy) defined
o.puts "Defined: #{what.ancestors.join('<-')} at #{where}"
else
# Report file loaded
o.puts "Loaded: #{what} at #{where}"
end
end
}
class Object
def trace(name="", stream=STDERR)
TracedObject.new self, name, stream
end
end
class TracedObject
instance_methods.each do |m|
m = m.to_sym
next if m == :object_id || m == :__id__ || m == :__send__
undef_method m
end
def initialize(o, name, stream)
@o = o
@n = name
@trace = stream
end
def method_missing(*args, &block)
m = args.shift
begin
arglist = args.map {|a| a.inspect}.join(', ')
@trace << "Invoking: #{@n}.#{m}(#{arglist}) at #{caller[0]}\n"
r = @o.send m, *args, &block
@trace << " Returning: #{r.inspect} from #{@n}.#{m} to #{caller[0]}\n"
r
rescue Exception => e
@trace << "Raising: #{e.class}:#{e} from #{@n}.#{m}\n"
#raise
end
end
def __delegate
@o
end
end
#a = [1,2,3].trace("a")
#a.reverse
#puts a[2]
#puts a.fetch(3)
puts '-------------'
class Module
def attributes(hash)
hash.each_pair do |symbol, default|
getter = symbol
setter = :"#{symbol}="
variable = :"@#{symbol}"
define_method getter do
if instance_variable_defined? variable
instance_variable_get variable
else
default
end
end
define_method setter do |value|
instance_variable_set variable, value
end
end
end
end
class Point
attributes :x => 0, :y => 0
end
point = Point.new
point.x=10
p point.instance_variables
puts '-------------'