I have a module like this: module Controller module LocaleModels def self.included(base) base.send :include, InstanceMethods end module InstanceMethods def locale_Lexeme; constantize_model(''Lexeme'') end def locale_Synthetic; constantize_model(''Synthetic'') end def locale_Property; constantize_model(''Property'') end private def constantize_model(common_part) eval(I18n.locale.capitalize + ''::'' + common_part).constantize end end end end But I kept getting NoMethodError (undefined method `constantize'' for #<Class:0x2483b0c>) I guess I cannot use ''constantize'' in a custom module. But can you please offer some workaround?
Hello-- On Aug 9, 2009, at 9:39 AM, boblu wrote:> > I have a module like this: > > module Controller > module LocaleModels > def self.included(base) > base.send :include, InstanceMethods > end > > module InstanceMethods > def locale_Lexeme; constantize_model(''Lexeme'') end > def locale_Synthetic; constantize_model(''Synthetic'') end > def locale_Property; constantize_model(''Property'') end > > private > def constantize_model(common_part) > eval(I18n.locale.capitalize + ''::'' + common_part).constantize > end > end > end > end > > But I kept getting > > NoMethodError (undefined method `constantize'' for #<Class:0x2483b0c>) > > I guess I cannot use ''constantize'' in a custom module. > > But can you please offer some workaround?This may not be the problem, but why are you using eval? ActiveSupport::CoreExtensions::String::Inflections defines constantize as a method on String. It would seem to me that this would work: (I18n.locale.capitalize + ''::'' + common_part).constantize However, if you are looking for a class, investigate classify instead. Maybe there''s something I''m missing here that requires use of eval? What happens in script/console if you type the expression in?
Thank you for your reply. Actually, that eval is a typo. But I found by removing that ''eval'' did resolved my problem here. Thank you very much. On 8月10日, 午前2:55, "s.ross" <cwdi...@gmail.com> wrote:> Hello-- > On Aug 9, 2009, at 9:39 AM,bobluwrote: > > > > > > > I have a module like this: > > > module Controller > > module LocaleModels > > def self.included(base) > > base.send :include, InstanceMethods > > end > > > module InstanceMethods > > def locale_Lexeme; constantize_model(''Lexeme'') end > > def locale_Synthetic; constantize_model(''Synthetic'') end > > def locale_Property; constantize_model(''Property'') end > > > private > > def constantize_model(common_part) > > eval(I18n.locale.capitalize + ''::'' + common_part).constantize > > end > > end > > end > > end > > > But I kept getting > > > NoMethodError (undefined method `constantize'' for #<Class:0x2483b0c>) > > > I guess I cannot use ''constantize'' in a custom module. > > > But can you please offer some workaround? > > This may not be the problem, but why are you using eval? > ActiveSupport::CoreExtensions::String::Inflections defines constantize > as a method on String. It would seem to me that this would work: > > (I18n.locale.capitalize + ''::'' + common_part).constantize > > However, if you are looking for a class, investigate classify instead. > > Maybe there''s something I''m missing here that requires use of eval? > What happens in script/console if you type the expression in?