I have a task where I have elder class and subclasses. Here is simplified code:
class Animal
attr_accessor :name, :name_list
def initialize
@name = name
@name_list = []
end
def set_name
@name = gets
puts "My name is #{@name}"
@name_list.append(@name)
end
def self.show_all
puts "List of your animals names: #{@name_list}"
end
end
class Dog < Animal
end
class Cat < Animal
end
dog1 = Dog.new
dog1.set_name
cat1 = Cat.new
cat1.set_name
Animal.show_all
So the question is: if all of the subclasses (cat and dog) have the same element - name(which is places in Animal class), how can I collect all of them in one array? Because if I just put name_list in set_name def - array appears to be empty. Is there any way to make it without writing another array outside the classes?
pets = [dog1, cat1]andpet_names = pets.map(&:name). - Tom Lordanimalsshould be an object that CONTAINSdog1andcat1, not be the the base class itself. - Tom Lord