Rails extends Ruby with mattr_accessor (Module accessor). As Ruby''s attr_accessor generates getter/setter methods for instances, mattr_accessor provide getter/setter methods at the module level. In below example, you see that mattr_accessor declared in the class context of LookupContext. It''s declared in class, not module. However, modules are defined in LookupContext, for example, the module ViewPaths, which makes use of the accessor. So is it safe to say that if a module accessor is declared in class, then it can only be available to modules of that class? class LookupContext mattr_accessor :fallbacks module ViewPaths self.class.fallbacks.each do |resolver| -- 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 https://groups.google.com/groups/opt_out.
it looks as if it is onyl available on the class because if you look it says self.class.fallbacks, yet that wouldnt make sense either because then what would be the point of its existance if you had to reach the class for it it, why not just create a class level macro instead? On Sep 9, 3:10 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> Rails extends Ruby with mattr_accessor (Module accessor). As Ruby''s > attr_accessor generates getter/setter methods for instances, > mattr_accessor provide getter/setter methods at the module level. In > below example, you see that mattr_accessor declared in the class > context of LookupContext. It''s declared in class, not module. However, > modules are defined in LookupContext, for example, the module > ViewPaths, which makes use of the accessor. So is it safe to say that > if a module accessor is declared in class, then it can only be > available to modules of that class? > > class LookupContext > mattr_accessor :fallbacks > module ViewPaths > self.class.fallbacks.each do |resolver|-- 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 https://groups.google.com/groups/opt_out.
John Merlino wrote in post #1075232:> Rails extends Ruby with mattr_accessor (Module accessor). As Ruby''s > attr_accessor generates getter/setter methods for instances, > mattr_accessor provide getter/setter methods at the module level. In > below example, you see that mattr_accessor declared in the class > context of LookupContext. It''s declared in class, not module.A class is a module.> However, > modules are defined in LookupContext, for example, the module > ViewPaths, which makes use of the accessor. So is it safe to say that > if a module accessor is declared in class, then it can only be > available to modules of that class? >What is ''it''? class LookupContext def LookupContext.fallbacks @@fallbacks end def LookupContext.fallbacks=(arr) @@fallbacks = arr end module ViewPaths def ViewPaths.greet p LookupContext.fallbacks end end end class Dog def bark p LookupContext.fallbacks end end LookupContext.fallbacks = [10, 20, 30] p LookupContext.fallbacks LookupContext::ViewPaths.greet Dog.new.bark In this code: module ViewPaths self.class.fallbacks.each do |resolver| ...self is the module LookupContext::ViewPaths, and self.class is Module, and Module has no method ''fallbacks'': lass LookupContext def LookupContext.fallbacks @@fallbacks end def LookupContext.fallbacks=(arr) @@fallbacks = arr end module ViewPaths puts self puts puts self.class puts puts self.class.fallbacks end end --output:-- LookupContext::ViewPaths Module 1.rb:60:in `<module:ViewPaths>'': undefined method `fallbacks'' for Module:Class (NoMethodError) from 1.rb:57:in `<class:LookupContext>'' from 1.rb:48:in `<main>'' -- 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 https://groups.google.com/groups/opt_out.