从irb导入(要求)Ruby类

Chr*_*ten 0 ruby irb

在Python中,将类,方法,变量等导入python shell是由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
Run Code Online (Sandbox Code Playgroud)

我可以通过调用将模块导入到Ruby shell中require '/path/to/module/search,但是当我尝试ApartmentSearch通过调用创建一个新对象时Search.ApartmentSearch.new(),我得到以下错误:NoMethodError: undefined method 'ApartmentSearch' for Search:Module这在某种程度上是显而易见的,因为它ApartmentSearch是一个类,而不是一个方法.我在这做错了什么?我的目标是让课程保持模块化并在需要时导入.

bjh*_*aid 6

你想做的事:

Search::ApartmentSearch.new() and not Search.ApartmentSearch.new()
Run Code Online (Sandbox Code Playgroud)

ApartmentSearchSearch命名空间中,因为ruby 中的模块可用于命名空间