class A
def a_method
puts "Definition in class A"
end
end
class B < A
def a_method
puts "Definition in class B (subclass of A)"
end
end
class C
def call_original
A.instance_method(:a_method).bind(self).call
end
end
c = C.new
c.call_original
I get the following error:
TypeError: bind argument must be an instance of A
method bind in c.rb at line 13
method call_original in c.rb at line 13
at top level in c.rb at line 17
Can someone please tell me why it is failing? TIA.
class A
def a_method
puts "Definition in class A"
end
end
class B < A
def a_method
puts "Definition in class B (subclass of A)"
end
end
class C
def call_original
A.instance_method(:a_method).bind(self).call
end
end
c = C.new
c.call_original
I get the following error:
TypeError: bind argument must be an instance of A
You've combined two related examples, but you've left out an important
bit:
class C < B
end
(on page 358) The class C declaration on p. 359 is a reopening of C,
so it doesn't have the < B part. If you want to do it all together,
just change "class C" to "class C < B".
The first puts statement calls const_missing() which returns 1 via
const_set(). puts takes it and prints it. The second time puts is
called, C::A is defined to 1, so the statement functions as you would
expect it.