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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-11 01:22 UTC
Re: Ruby For Rails Bind and Unbind question
Hi -- On Tue, 10 Oct 2006, Bala Paranj wrote:> > 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 AYou''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 | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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
> You''ve combined two related examples, but you''ve left out
an important
> bit:
>
> class C < B
> end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-11 11:55 UTC
Re: Ruby For Rails Bind and Unbind question
Hi -- On Tue, 10 Oct 2006, Bala Paranj wrote:> > 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 undefinedsetting it to 1." > const_set(const,1) > end > end > puts C::A > puts C::AIt''s to show you that the first time, it calls const_missing and gives you the longer message, but the second time, the constant is no longer missing so it just prints 1. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---