所以我试图创建一个由多个类组成的数组,我称之为Homes
但是当我运行我的代码时,我的数组只包含一个Home
我在Rails框架中这样做。
class HomesController < ApplicationController
def index
home_names_array = ['Kodu 1' 'Kodu 2']
homes = []
home_names_array.each do |home|
x = Homes.new
x.home_name = home
x.create_apartments(['Korter 56', 'Korter 78'])
homes << x
end
homes.each do |x|
logger.debug homes.size
logger.debug "Home name: #{x.get_name}\n"
end
end
class Home
attr_accessor :home_name
APARTMENTS = []
def create_apartments(apartment_names)
apartment_names.each do |apartment|
x = Apartment.new
x.apartment_name = apartment
APARTMENTS << x
end
end
def get_name
home_name
end
def get_apartments
APARTMENTS.each do |apartment|
apartment
end
end
end
class Apartment
attr_accessor :apartment_name
def get_apartment_name
apartment_name
end
end
end
在索引操作中,它应该将x这是一个类推送到我的数组。但结果只是一个变量,其中包含一个类。
答案 0 :(得分:3)
您在home_names_array
中遗漏了逗号。它应该是home_names_array = ['Kodu 1', 'Kodu 2']