I have a system in which the user can create a new object of the class Map that has a subclass of SubMap. The thing is, there can be multiple Map objects, and for each one of those, there can be multiple SubMap objects. I need to be able to create a SubMap object that can both get and change the Map object's @b value of the unique ID passed during the SubMap instantiation. Ex:
map1=Map.new(10,5) #Map is assigned unique ID of 1 and has a few instance variables (@b = 5)
point=Map::SubMap.new(1) #Assigned to the map object with an ID of 1, which is map1
point.change_b(8) #Change map1's @b value to 8.
Here is an expanded code example:
class Map
@@id=1
def change_b(b)
@b=b
end
def initialize(a,b)
@id=@@id
@@id+=1
@a=a
@b=b
@map="#{a}_#{b}"
end
end
class SubMap < Map
def initialize(mapId)
@mapID=mapId
end
def change_b(b)
super
end
end
map=Map.new(5,2) #Create New Map ID: 1, @b = 2
second_map=Map.new(6,1) #Create Test Map ID: 2, @b = 1
point=Map::SubMap.new(1) #Just affect the map with the ID of 1
point.change_b(5) #Change map's (Instance of Map with ID of 1) instance variable of @b to 5
##second_map's @b value is unchanged.
I'll welcome any other methods for doing this (no pun intended), Thanks in advance. Also, I apologize for any formatting errors (Missing indents and such that I may have missed), lets just say me and the SO code format didn't agree on a few things.