Ruby For Rails Bind and Unbind question

When I run the following example:

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.

Hi --

When I run the following example:

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".

David

Thank you. In the book example on const_missing, why does the output print 1 twice?

class C   def self.const_missing(const)     puts "#{const} is undefined—setting it to 1."     const_set(const,1)   end end puts C::A puts C::A

Why wouldn't it?

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.

Hi --