0

I would like to be able to do this in Ruby:

module A
  @var = Object.new  # could be any object
  def f
    puts @var.inspect
  end
  module_function :f
end

module B
  def make_it(val)
    # creates reference to val in this module under the same name as
    # it appears in module A
  end
  module_function :make_it
end

So that this would make module B respond as does module A:

B.make_it(# a reference to @var in module A)
B.make_it(# a reference to f in module A)

This would allow B.f to act like A.f and B.f would return the 'inspect' of @var in module A or the 'inspect' of @var in B where it is a copy of that in A.

I have asked for references be made in B, but could live with copies as well.

Anyone know if this can be done? To have an example would be wonderful, but any assistance would be much appreciated. I have googled quite a bit without luck - perhaps I don't know how to phrase the search.

Dave Inman
  • 34
  • 4

1 Answers1

0

I'm not sure why you want to do this. But, looking at it from the outside, I'm not sure it's a good idea. As they say here modules are about functions. But they are also like libraries, so ideally you would want them to be relatively independent of each other and not actually reaching into one another. This would also make things better in the long run by reducing interdependence and the possibility of broken code.

Perhaps if you could merge the two modules? Or simply provide duplicate code in each?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • The purpose of mulitple modules is to have one for the program's use and another for data from the user environment that gets eval'd. I want to make certain methods available to the user environment (namespace) and I want it to be done dynamically. These methods are always available to non-eval'd side of the app. – Dave Inman Oct 19 '15 at 20:50