In article <Pine.LNX.4.33.0301212020500.6373-100000 / eli.fsl.noaa.gov>,
ahoward  <ahoward / fsl.noaa.gov> wrote:
>On 21 Jan 2003, Phil Tomson wrote:
>
>> I've got a module that that has several classes defined within it.  These
>> classes need to access some instance variables defined at the module
>> level.
>>
>> For example:
>>
>> module Evolvable
>>   PC = 0.2
>>   PM = 0.01
><snip>
>> end
>>
>> 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?
>
>i'm unsure of the semantics of instance variables in a module (something you
>cannot declare an instance of) 

Actually they seem to be allowed (perhaps 'module instance variable' isn't 
the right term, though) but they must be set and accessed through module 
methods (def ModuleName.methodname....).


>but assuming you need both the module defaults
>AND 'instance variable' in your module (perhaps because you will want to tweak
>these 'instance variables' later and yet have sensible defaults) why not
>simply :
>
>
>  class Evolvable
>    require 'singleton'
>    include Singleton
>
>  private
>
>    PC = 0.2
>
>    def initialize
>      @pc = PC
>    end
>
>    attr_writer :pc
>
>  public
>
>    attr_reader :pc
>  end
>
>
>  e = Evolvable.instance
>  puts e.pc 		# >> 0.2
>
>perhaps PC should be @@pc or something, but you get the picture :
>
>by putting the vars in a class you can use attr_*, but making the class
>a Singleton you ensure only one copy of @pc will ever exist.
>
>-a
>

That's certianly one way to do it...  I guess I'm looking at Evolvable as 
more of a namespace than a class that defines an object, though.  And if I 
leave it as a module it could be mixed into a class later (though that's 
not how I'm using it now) and all of the 'module instance variables' would 
also end up in the class it was mixed into.... though there might be some 
issues with mixing in the module methods.

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