Hi --

On Mon, 14 Apr 2003, Tim Bates wrote:

> Hi all,
> I have several classes in a module, thus:
>
> module Example
>   class Ay
>   end
>
>   class Bee
>   end
> end
>
> I have a piece of data I want to share between classes Example::Ay and
> Example::Bee, preferably without it being available to things outside the
> module. What's the best way to do this? As a class variable of Example
> doesn't seem to work, as I can't access this from within Ay or Bee...

Here's a start:

  module Example
    module Secret
      class << self; attr_accessor :s; end
      def s; Secret.s; end
      def s=(n); Secret.s = n; end
    end

    class Ay
      include Secret
    end

    class Bee
      include Secret
    end
  end

  Example::Ay.new.s = 10
  p Example::Bee.new.s  # => 10

You could also put a class variable in Secret and write accessor
wrappers for it (instead of an instance variable of Secret, which is
what I've done).

I don't like repeating "Secret" in the method definitions; it feels
like there should be a more heuristic way to do that.


David

-- 
David Alan Black
home: dblack / superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav