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 –

On Tue, 10 Oct 2006, Bala P. wrote:

puts “Definition in class B (subclass of A)”
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


David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs