From: "jan aerts (RI)" Date: 2006-11-13T23:50:55+09:00 Subject: classes within classes Hi all, I'm trying to create a little library for drawing my data using SVG. One of the inherent properties of the drawings that I have to make, is that each picture can contain one or more tracks with each track containing one or more features (see ASCII art below). +----------------------------picture-+ | +----------------------track1----+ | | | | | | | x x | | | | x x | | | | | | | | | | | +--------------------------------+ | | | | +----------------------track2----+ | | | | | | | xx | | | +--------------------------------+ | +------------------------------------+ As a track can _only_ be defined within a picture, and a feature can _only_ be defined within a track, I have set up the classes as follows: class Picture class Track class Feature end end end The problem is that some of the properties of a Picture object have to be readable for its Track objects. This is _not_ simple inheritance, because Picture and Picture::Track are two completely different things. As I see it, there are several options: (A) either pass those properties as arguments every time you create a new Picture::Track object. Not optimal, because the same piece of data is copied over and over again. (B) set those features as global variables. Not optimal as well. (C) something more elegant? More elaborately (showing some of the key features) class Picture def initialize(width) @width = width @tracks = Array.new end attr_accessor :width, :tracks def add_track(name) @tracks.push(Picture::Track.new(name) return @tracks[-1] end class Track # DO NOT CALL INITIALIZE METHOD DIRECTLY: use Picture#add_track def initialize(name) @name = name end attr_accessor :name def to_svg # I NEED THE WIDTH OF THE PICTURE HERE return some_xml_that_includes_the_width_which_was_defined_in_Picture end class Feature ... end end end p = Picture.new(800) # Creates a new picture of 800pt width. p.add_track('first_track') p.add_track('second_track') In the end, the question is: should I make Picture#width a global variable, should I add width as an argument to Picture::Track#initialize, or is there a more elegant solution? (I hope it's the last one...) Any help would be very much appreciated, jan. Dr Jan Aerts Bioinformatics Group Roslin Institute Roslin, Scotland, UK +44 131 527 4200 ---------The obligatory disclaimer-------- The information contained in this e-mail (including any attachments) is confidential and is intended for the use of the addressee only. The opinions expressed within this e-mail (including any attachments) are the opinions of the sender and do not necessarily constitute those of Roslin Institute (Edinburgh) ("the Institute") unless specifically stated by a sender who is duly authorised to do so on behalf of the Institute.