I'm trying to use Ruby classes that are never instantiated, but still have the concept of inheritance.So looking at the code below, I have class "Room" that allows access to variables without instantiating the class (I have them as instance variables, but I'm not sure that's correct).
But I want to be able to further constrain the "Room" class into different types of room. And then I want each subclass to have its own variables (@log, @occpants). How would I do this?
If I use class variables, they would be overwritten for each class each time one was changed.
class Room
@log = []
@occupants = []
def self.occupants
@occupants
end
def self.log
@log
end
def self.log_entry(person)
if @occupants << person
@log << "#{person.name} entered Office: #{Time.now}"
end
end
def self.log_exit(person)
if @occupants.delete(person)
@log << "#{person.name} exited Office: #{Time.now}"
end
end
end
class Office < Room
end
class Kitchen < Room
end
Roomis an instance of theClassclass, while classOfficewill become another instance ofClass, look here how it works.