hi again, all. I have the following plugin included in my active record model. What i''d like to do, is define a method inside class_eval which returns the options has that was passed into it via the acts_as_item :foo=>:bar in my model. What i''m finding odd is that the self.foo method can access the config hash when printing it, but not when i try to return it. puts "OK FOO: #{config[:asset_root]}" - outputs ok, whereas the return config raises a method or variable not found error. Any ideas? best, paul. module ClassMethods def acts_as_item(options = {}) include InstanceMethods config = {:asset_root=>"SOMEPATH"} config.update(options) if options.is_a?(Hash) class_eval <<-EOE def self.foo puts "OK FOO: #{config[:asset_root]}" return config end EOE end end -- 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.
On Feb 25, 6:28 pm, Paul PH <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> hi again, all. > I have the following plugin included in my active record model. > What i''d like to do, is define a method inside class_eval which returns > the options has that was passed into it via the acts_as_item :foo=>:bar > in my model. > > What i''m finding odd is that the self.foo method can access the config > hash when printing it, but not when i try to return it. >Two very different things are happening: with the puts config is being resolved just before class_eval is called: the string is interpolated and the result passed to class_eval. The return statement on the other hand is evaluated much later - when the foo method is actually called. config is no longer in scope and so you get an error. Fred> puts "OK FOO: #{config[:asset_root]}" - outputs ok, whereas the return > config raises a method or variable not found error. > > Any ideas? > best, > paul. > > module ClassMethods > def acts_as_item(options = {}) > include InstanceMethods > > config = {:asset_root=>"SOMEPATH"} > config.update(options) if options.is_a?(Hash) > > class_eval <<-EOE > def self.foo > puts "OK FOO: #{config[:asset_root]}" > return config > end > EOE > end > end > -- > 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-/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 wrote:> On Feb 25, 6:28�pm, Paul PH <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> hi again, all. >> I have the following plugin included in my active record model. >> What i''d like to do, is define a method inside class_eval which returns >> the options has that was passed into it via the acts_as_item :foo=>:bar >> in my model. >> >> What i''m finding odd is that the self.foo method can access the config >> hash when printing it, but not when i try to return it. >> > Two very different things are happening: with the puts config is being > resolved just before class_eval is called: the string is interpolated > and the result passed to class_eval. > The return statement on the other hand is evaluated much later - when > the foo method is actually called. config is no longer in scope and so > you get an error. > > FredAh, I see - thanks Fred. I understand the variable is resolved earlier to do the string interpolation. I do find it odd that it is put into scope for that, but not for returning it. Thanks, again. Paul. -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.