在Python中,将类、方法、变量等导入到python是由import MyClass完成的。我要在Ruby shell中做同样的事情。我有以下代码:
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是一个类,而不是一个方法。我在这里做错什么了?我的目标是使类保持模块化,并在需要时导入。
发布于 2014-02-12 00:00:38
你想做的是:
Search::ApartmentSearch.new() and not Search.ApartmentSearch.new()ApartmentSearch在Search名称空间中,因为ruby中的模块可以用于名称空间。
https://stackoverflow.com/questions/21715748
复制相似问题