Vidmantas Kabošis
2008-Oct-07 18:19 UTC
Pass option value to InstanceMethods define_method
Hello, I''m trying to develop a ActiveRecord plugin but run into the following problem: I pass arguments to write_inheritable_attribute :some_field, ''value'' class_inheritable_reader :some_field in ClassMethods module and I''m available to get them in InstanceMethods with self.class.some_field. But when I try to do like this: define_method "#{self.class.some_field}" ... or class_eval <<-eval def #{self.class.some_field} ... end eval I get NoMethodError: undefined method `some_field'' for Module:Class Anybody could help with this? -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Oct-07 18:24 UTC
Re: Pass option value to InstanceMethods define_method
On Oct 7, 7:19 pm, Vidmantas Kabošis <rails-mailing-l...@andreas- s.net> wrote:> Hello, > > I''m trying to develop a ActiveRecord plugin but run into the following > problem: I pass arguments to > > write_inheritable_attribute :some_field, ''value'' > class_inheritable_reader :some_field > > in ClassMethods module and I''m available to get them in InstanceMethods > with self.class.some_field. But when I try to do like this: >I may not be reading you correctly, but at that point do you not need to be using self.some_field ? (self.class.some_field would be trying to find that method on Class or something like that. Fred> define_method "#{self.class.some_field}" > ... > > or > > class_eval <<-eval > def #{self.class.some_field} > ... > end > eval > > I get NoMethodError: undefined method `some_field'' for Module:Class > > Anybody could help with this? > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Vidmantas Kabošis
2008-Oct-08 07:36 UTC
Re: Pass option value to InstanceMethods define_method
Frederick Cheung wrote:> using self.some_field ?I''ve tried this too, but then I get NoMethodError: undefined method `some_field'' for MyModule::InstanceMethods:Module -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Oct-08 07:44 UTC
Re: Pass option value to InstanceMethods define_method
On Oct 8, 8:36 am, Vidmantas Kabošis <rails-mailing-l...@andreas- s.net> wrote:> Frederick Cheung wrote: > > using self.some_field ? > > I''ve tried this too, but then I get NoMethodError: undefined method > `some_field'' for MyModule::InstanceMethods:ModuleI think you need to post more of your code. It''s not clear where you are calling what which is rather important here. 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-/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?hl=en -~----------~----~----~----~------~----~------~--~---
Vidmantas Kabošis
2008-Oct-08 10:11 UTC
Re: Pass option value to InstanceMethods define_method
Frederick Cheung wrote:> I think you need to post more of your code.Sure: module MyModule def self.included(base) base.extend ClassMethods end module ClassMethods def money_is(field_name) write_inheritable_attribute :some_field, ''value'' class_inheritable_reader :some_field self.send(:include, MyModule::InstanceMethods) end end module InstanceMethods define_method "#{self.some_field}" do # code end end end> script/consoleLoading development environment (Rails 2.1.0) /home/vidmantas/projects/my_project/vendor/plugins/my_module/lib/my_module.rb:16:NoMethodError: undefined method `some_field'' for MyModule::InstanceMethods:Module -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Oct-08 10:18 UTC
Re: Pass option value to InstanceMethods define_method
On 8 Oct 2008, at 11:11, Vidmantas Kabošis wrote:> > Frederick Cheung wrote: >> I think you need to post more of your code. > > Sure: > > module MyModule > def self.included(base) > base.extend ClassMethods > end > > module ClassMethods > def money_is(field_name) > write_inheritable_attribute :some_field, ''value'' > class_inheritable_reader :some_field > > self.send(:include, MyModule::InstanceMethods) > end > end > > module InstanceMethods > define_method "#{self.some_field}" do > # code > end > end > endOK. when the define_method runs, self is the module InstanceMethods. At that point it hasn''t even been included in anything so there is no way to get the value of some_field. I would just be doing this from the money_is method (or if you want to keep the InstanceMethods module then from its included hook. Fred> > >> script/console > Loading development environment (Rails 2.1.0) > /home/vidmantas/projects/my_project/vendor/plugins/my_module/lib/ > my_module.rb:16:NoMethodError: > undefined method `some_field'' for MyModule::InstanceMethods:Module > > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Vidmantas Kabošis
2008-Oct-08 13:03 UTC
Re: Pass option value to InstanceMethods define_method
Got it, thank you! Frederick Cheung wrote:> I would just be doing this from the money_is method (or if you want to > keep the InstanceMethods module then from its included hook.-- 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?hl=en -~----------~----~----~----~------~----~------~--~---