-2
所以我想創建一個由多個類,我稱之爲Homes
無法使類的數組中的Ruby
但是當我運行我的代碼,我的陣列只包含一個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這是一個類推到我的數組中。但結果只是一個類中的變量。
您是否嘗試調試該問題? – akonsu 2014-10-29 14:24:59