-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobj_20.rb
executable file
·48 lines (40 loc) · 892 Bytes
/
robj_20.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
#!/usr/bin/env ruby
require "./ruler"
#-----------------------------------------------------------------------------
R("")
class MethodLogger
def log_method(klass, method_name)
klass.class_eval do
puts "> Trying to define #{method_name}"
alias_method "#{method_name}_old", method_name
define_method "#{method_name}" do |*args, &block|
puts "# DBG: #{method_name} called"
send "#{method_name}_old", args, block
end
end
end
end
class Image
attr_accessor :name, :x, :y
def initialize(name)
@name = name
@x = 10
@y = 20
end
def to_s
"# Image name: #{@name}, x:#{@x}, y:#{@y}"
end
def move_xy(xx, yy)
@x, @y = xx, yy
puts "# Moved to #{@x} and #{@y}"
end
end
R("Before move_xy is traced")
i = Image.new("kossak")
puts i
i.move_xy(1, 1)
puts i
R("After move_xy is traced")
ml = MethodLogger.new
ml.log_method(Image, :move_xy)
i.move_xy(-10, -5)