From: Joel Pearson Date: 2013-07-02T17:12:52+09:00 Subject: Re: How do I make lots of classes aware of each other? Perhaps an example would help. I struggled for a while with the links between classes in an Excel-style structure that I made. Here's the documentation: http://rubydoc.info/gems/rubyexcel/0.2.7/index This is the structure I used: All classes are namespaced under "module RubyExcel" "class Workbook" holds multiple instances of "class Sheet" in an instance variable called @sheets. Method "Workbook#sheets" allows you to loop through each sheet or select specific ones with an argument. Each instance of "class Sheet" holds a reference to its "parent" Workbook, and its data, held in "class Data". The Data and its link Each instance of classes Row, Column, Range, and Cell hold references to their "parent" Sheet, which in turn provides a path to the Data and the Workbook. Because these classes exist merely to access specific sections of the Sheet/Data, they can be garbage collected once they're finished with, and don't need persistent tracking since all they do is reference sections of the Data. They can be recreated when required. When I say "parent", I'm not talking about class inheritance, merely an instance variable which holds a reference to an instance of another class which is associated with the instance of the current class. Here's a simple example of the structure (without all the long object ids in the output): class Parent attr_accessor :children def initialize @children = [] end end class Child attr_accessor :parent def initialize( parent ) @parent = parent parent.children << self end end parent = Parent.new child1 = Child.new parent child2 = Child.new parent parent.children => child1, child2 child1.parent => parent child2.parent => parent -- Posted via http://www.ruby-forum.com/.