0
在Python中,將類,方法,變量等導入python shell由import MyClass
完成。我想在Ruby shell中做同樣的事情。我有以下代碼:從irb導入(需要)Ruby類
module Search
class ApartmentSearch
attr_accessor :hood, :minPrice,
:maxPrice, :bed,
:bath
def initialize(hood = "",
minPrice = 0,
maxPrice = 0,
bed = 0,
bath = 0)
@hood = hood
@minPrice = minPrice
@maxPrice = maxPrice
@bed = bed
@bath = bath
end
end
end
我可以require '/path/to/module/search
導入模塊與Ruby的外殼,但是當我再嘗試通過調用Search.ApartmentSearch.new()
,我收到以下錯誤創建一個新的ApartmentSearch
對象:NoMethodError: undefined method 'ApartmentSearch' for Search:Module
,這是因爲ApartmentSearch
是一類,而不是一種方法,所以在某種程度上是明顯的。我在這裏做錯了什麼?我的目標是讓課程保持模塊化並在需要時進口。
你想做的事:'搜尋:: ApartmentSearch.new()'和而不是'Search.ApartmentSearch.new()' – bjhaid
啊,那很好。我沒有發現當我搜索文檔時,但我想我回想起使用類似網絡的東西:HTTP.method –
@bjhaid:您的評論應該是(接受的)答案 – Johnsyweb