From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2003-01-22T12:59:28+09:00 Subject: Re: accessors for module instance vars In article <3E2DA7AD.6040604@path.berkeley.edu>, Joel VanderWerf wrote: >Phil Tomson wrote: > >> Now this works ok, but I'm wondering if there's a way to create the >> accessor methods for the Evolvable module automatically like with >> attr_accessor and attr_reader. The problem with attr_accessor is that it >> creates an instance method, not a module method - is there a way to >> automatically create a module method? >> >> Phil >> >> >irb(main):001:0> module M >irb(main):002:1> class << self; attr_accessor :foo; end >irb(main):003:1> end >nil >irb(main):004:0> M.foo >nil >irb(main):005:0> M.foo = 4 >4 >irb(main):006:0> M.foo >4 > > Great! I wasn't sure if that would work. Actually, about an hour after I posted I had a different idea which means that I cant' use attr_accessor anyway. I decided that if the modules hadn't been 'initialized' that the 'get' methods should do some sort of default initialization: module Evolvable XOVER_DEFAULT = :uniform #..... def Evolvable.set_crossoverType(type=XOVER_DEFAULT) @crossoverType = type end def Evolvable.get_crossoverType if not defined? @crossoverType #default in case it hasn't been set: @crossoverType = XOVER_DEFAULT end @crossoverType end end ....so I can't use attr_accessor anyway if I want this kind of functionality. Phil -- "Or perhaps the truth is less interesting than the facts?" Amy Weiss (accusing theregister.co.uk of engaging in 'tabloid journalism') Senior VP, Communications Recording Industry Association of America