you want to do:
Search::ApartmentSearch.new() and not Search.ApartmentSearch.new()
ApartmentSearch
is in the Search
namespace as modules in ruby can be used for namespacing
题
In Python, importing classes, methods, variables etc to the python shell is done by import MyClass
. I was to do the same thing in the Ruby shell. I have the following code:
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
I can import the module into the Ruby shell by require '/path/to/module/search
, but when I then try to create a new ApartmentSearch
object by calling Search.ApartmentSearch.new()
, I get the following error: NoMethodError: undefined method 'ApartmentSearch' for Search:Module
, which is obvious to some degree because ApartmentSearch
is a class, not a method. What am I doing wrong here? My goal is to have classes kept modular and imported where needed.
解决方案
you want to do:
Search::ApartmentSearch.new() and not Search.ApartmentSearch.new()
ApartmentSearch
is in the Search
namespace as modules in ruby can be used for namespacing