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
|
p Struct.singleton_methods(false)
class Class
alias :old_new :new
def new *a
puts "Class#new: #{self}.new(#{a.inspect})"
begin
x = old_new *a
rescue
puts "!"
p $!
end
x
end
end
class Struct
alias :init :initialize
def initialize *a
puts "Struct#initialize: #{a.inspect}"
puts " #{self.inspect}"
init *a
end
#class << self
# remove_method :new
#end
end
class S < Struct
def initialize *a
puts "S#initialize: #{a.inspect}"
puts " #{self.inspect}"
super
end
end
p Struct.singleton_methods(false)
p Class.instance_methods(false)
begin
puts '# no initializer called yet'
I = S.new(:foo, :bar)
class I
p superclass
p singleton_methods(false)
p instance_methods(false)
p private_instance_methods(false)
class << self
alias :old_new :new
def new *a
puts "I#new: #{self}.new(#{a.inspect})"
old_new *a
end
def [] *a
puts "I#[]: #{self}.[](#{a.inspect})"
old_new *a
end
end
def initialize *a
puts "I#initialize: #{a.inspect}"
puts " #{self.inspect}"
super
end
end
puts "---"
puts '# now it calls initializer (via new)'
p I.new("x", "y")
puts "---"
puts '# now it calls initializer (via [])'
p I["x", "y"]
puts "---"
puts '# remove new methods'
class I
class << self
remove_method :new
end
end
p I.new("U", "V") # calls Struct#new
puts '---'
class Struct
class << self
remove_method :new
end
end
p I.new("U", "V") # creates the struct instance :)
puts '---'
rescue
p $!
end
|