Hi, thank you for reading my post. I''ll shortly describe my problem. I want to probe if a class is defined, and if it is, I want to instantiate it. Let''s assume, the class name is "DemandType" and it exists: Object.const_defined?("DemandType") => false Object.const_get("DemandType") => Class Why does const_defined?() returns false in this case? I have also tested it in rails console, but it was not successful. I could also work with const_get() alone and catch the NameError exception, but I would really like to know why const_defined?() is not working. Thank you very much in advance! ms -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-May-11 22:19 UTC
Re: Why does Object.const_defined?() returns false in this case?
On May 11, 9:32 pm, ms <m...-cGBD8117FJM@public.gmane.org> wrote:> Object.const_defined?("DemandType") => false > Object.const_get("DemandType") => Class > > Why does const_defined?() returns false in this case? I have also > tested it in rails console, but it was not successful. I could also > work with const_get() alone and catch the NameError exception, but I > would really like to know why const_defined?() is not working. >Are you in development mode ? If so classes aren''t loaded until they are needed. const_defined? is returning false because the class does not exist in memory. const_get triggers Rails'' const_missing handler which loads the class (and so const_get can then return it) Fred -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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?hl=en.
Thanks for the quick answer! Yes, I am in development mode and this is actually the problem as you described. How can I force Rails even in development mode to load ALL classes, is that possible? Thank you, ms On 12 Mai, 00:19, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On May 11, 9:32 pm, ms <m...-cGBD8117FJM@public.gmane.org> wrote: > > > Object.const_defined?("DemandType") => false > > Object.const_get("DemandType") => Class > > > Why does const_defined?() returns false in this case? I have also > > tested it in rails console, but it was not successful. I could also > > work with const_get() alone and catch the NameError exception, but I > > would really like to know why const_defined?() is not working. > > Are you in development mode ? If so classes aren''t loaded until they > are needed. const_defined? is returning false because the class does > not exist in memory. const_get triggers Rails'' const_missing handler > which loads the class (and so const_get can then return it) > > Fred > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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?hl=en.
Frederick Cheung
2010-May-12 08:46 UTC
Re: Why does Object.const_defined?() returns false in this case?
On May 11, 11:53 pm, ms <m...-cGBD8117FJM@public.gmane.org> wrote:> Thanks for the quick answer! Yes, I am in development mode and this is > actually the problem as you described. How can I force Rails even in > development mode to load ALL classes, is that possible?As far as I know there''s not a nice way of doing this in general without losing class reloading If there''s only a subset of classes for which this is important you could stick a bunch of calls to require_dependency at the bottom of application_controller.rb> > Thank you, > ms > > On 12 Mai, 00:19, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > On May 11, 9:32 pm, ms <m...-cGBD8117FJM@public.gmane.org> wrote: > > > > Object.const_defined?("DemandType") => false > > > Object.const_get("DemandType") => Class > > > > Why does const_defined?() returns false in this case? I have also > > > tested it in rails console, but it was not successful. I could also > > > work with const_get() alone and catch the NameError exception, but I > > > would really like to know why const_defined?() is not working. > > > Are you in development mode ? If so classes aren''t loaded until they > > are needed. const_defined? is returning false because the class does > > not exist in memory. const_get triggers Rails'' const_missing handler > > which loads the class (and so const_get can then return it) > > > Fred > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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?hl=en.
ssssssssssssss Attachments: http://www.ruby-forum.com/attachment/4723/Scrap.shs -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rick DeNatale
2010-May-12 17:23 UTC
Re: Re: Why does Object.const_defined?() returns false in this case?
On Tue, May 11, 2010 at 6:53 PM, ms <ms-cGBD8117FJM@public.gmane.org> wrote:> Thanks for the quick answer! Yes, I am in development mode and this is > actually the problem as you described. How can I force Rails even in > development mode to load ALL classes, is that possible?It''s not really even a development vs production environment question. Rails in general loads missing constants when referenced. The difference in the development environment is that it cleans them out between requests. Why do you need to use const_defined? Just refer to the class by name and let Rails do it''s thing. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-May-12 18:17 UTC
Re: Why does Object.const_defined?() returns false in this case?
On May 12, 6:23 pm, Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, May 11, 2010 at 6:53 PM, ms <m...-cGBD8117FJM@public.gmane.org> wrote: > It''s not really even a development vs production environment question. > Rails in general loads missing constants when referenced. The > difference in the development environment is that it cleans them out > between requests. >Actually Rails loads just about everything ahead of time in production since 2.2 or 2.1 (don''t remember which) as part of the thread safeness work. The const_missing hook is still there, it is just unlikely to get used in production Fred> Why do you need to use const_defined? Just refer to the class by name > and let Rails do it''s thing. > > -- > Rick DeNatale > > Blog:http://talklikeaduck.denhaven2.com/ > Github:http://github.com/rubyredrick > Twitter: @RickDeNatale > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn:http://www.linkedin.com/in/rickdenatale > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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?hl=en.