When I run the following example on page 349, I get the error message: NoMethodError: undefined method `some_method'' for #<C:0x2e3d698 @x=1> from (irb):94 from ?:0 c = Class.new c.class_eval do def some_method puts "Created in class_eval" end end c = C.new c.some_method How to run this example? I followed the suggestion on the Manning''s errata page with no luck. TIA. A. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj <bparanj@...> writes:> c = Class.newYour non-capitalization above is incorrect. The new class should be a constant. Try C = Class.new> c.class_eval do> def some_method > puts "Created in class_eval" > end > end > c = C.newWell, you haven''t defined C because of the capitalization issue above.> c.some_method >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It works now. Why does it have to be constant?> > end > > c = C.new > Well, you haven''t defined C because of the capitalization issue above. > > c.some_method--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bala Paranj wrote:> It works now. Why does it have to be constant?It doesn''t really have to be. It''s just a convention that classes are defined into constants. You can just as easily do: x = Class.new x.class_eval do def some_method puts "Created in class_eval" end end c = x.new c.some_method The reason your first example failed was b/c you were inconsistently mixing ''c'' and ''C''. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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:09 UTC
Re: Ruby For Rails class_eval question
Hi -- On Tue, 10 Oct 2006, Bala Paranj wrote:> > When I run the following example on page 349, I get the error message: > > NoMethodError: undefined method `some_method'' for #<C:0x2e3d698 @x=1> > from (irb):94 > from ?:0 > > c = Class.new > c.class_eval do > def some_method > puts "Created in class_eval" > end > end > c = C.new > c.some_method > > How to run this example? I followed the suggestion on the Manning''s errata page with no luck. TIA.The erratum has an erratum.... The two lines I provided are run together into one. They should be split, as below. The whole thing should be: c = Class.new c.class_eval do def some_method puts "Created in class_eval" end end c_instance = c.new c_instance.some_method 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-11 01:10 UTC
Re: Ruby For Rails class_eval question
Hi -- On Wed, 11 Oct 2006, Kian Wright wrote:> > Bala Paranj wrote: >> It works now. Why does it have to be constant? > > It doesn''t really have to be. It''s just a convention that classes are > defined into constants. > > You can just as easily do: > > x = Class.new > x.class_eval do > def some_method > puts "Created in class_eval" > end > end > c = x.new > c.some_method > > The reason your first example failed was b/c you were inconsistently > mixing ''c'' and ''C''.My fault -- it was wrong in the first printing, and the erratum has two lines run together as one. (See my other reply on this thread for the working version.) 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 -~----------~----~----~----~------~----~------~--~---