Hi.

I have big module with many tiny methods. These methods
all return a string.

Example:


  module Foo

    def one(i = '')
      return '<'+i+'>'
    end

    def two(i = '')
      return '<'+i+i+'>'
    end

  end

So far so good. Please note that this is just a short
demo, a simplification. The real module is huge, and
much more complicated. But I just want to demo something
relevant for my question.

Now, I include this module into class bar.

class Bar

  include Foo

  attr_reader :string

  def initialize
    @string = ''
    build_string
  end
  def build_string
    @string << one('hello')
    @string << two('world')
  end
end

x = Bar.new
x.string

Ok this will return this string:

"<hello><worldworld>"

So far, everything works correctly.

As you can see, in class Bar I can use an @ivar,
in this case @string.

Now, however, I _also_ want to be able to store
this in the module. But how can I do this?

I could use a global variable, but this is awful.

I could use a constant, like X = [''] and
append to that in the module, but this is
also awful.

Why would I want this? Because a module is more
flexible than a class - I can use the module
to mix it into other classes AND I could use
the module standalone.

Any suggestion for how to store this information
in the module? I want to have the module give
me back the current version of the string it
uses. And I also must keep the core functionality
of including all those methods in the module
into my new, more specialized classes.

Any HELPFUL suggestions welcome!

-- 
Posted via http://www.ruby-forum.com/.