Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 铁轨:;NoMethodError:未定义的方法`ConstructionKDTree';对于#<;类别:0x0000104B1F760>&引用;_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 铁轨:;NoMethodError:未定义的方法`ConstructionKDTree';对于#<;类别:0x0000104B1F760>&引用;

Ruby on rails 铁轨:;NoMethodError:未定义的方法`ConstructionKDTree';对于#<;类别:0x0000104B1F760>&引用;,ruby-on-rails,ruby,Ruby On Rails,Ruby,您好,我在rails中有3个文件,如下所示: 1) 位于“app/controller/listings\u controller.rb” 现在我正在尝试测试constructKDTree的方法实现,因此我转到rails控制台并尝试了以下命令: 1.9.2-p290 :001 > @lc = ListingsController.new => #<ListingsController:0x00000104f3e288 @_routes=nil, @_action_has_la

您好,我在rails中有3个文件,如下所示: 1) 位于“app/controller/listings\u controller.rb”

现在我正在尝试测试
constructKDTree
的方法实现,因此我转到rails控制台并尝试了以下命令:

1.9.2-p290 :001 > @lc = ListingsController.new
 => #<ListingsController:0x00000104f3e288 @_routes=nil, @_action_has_layout=true, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil> 

1.9.2-p290 :002 > @lc.index
1.9.2-p290:001>@lc=ListingsController.new
=>#“text/html”}、@_status=200、@_request=nil、@#u response=nil>
1.9.2-p290:002>@lc.index
但我得到了这个错误:

NoMethodError: undefined method `constructKDTree' for #<Class:0x00000104b1f760>
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
from /Users/AM/Documents/RailsWS/cmdLineWS/Businesses/app/controllers/listings_controller.rb:20:in `index'
from (irb):2
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
NoMethodError:#
from/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active\u record/dynamic\u matchers.rb:50:in'method\u missing'
from/Users/AM/Documents/RailsWS/cmdLineWS/business/app/controllers/listings_controller.rb:20:in'index'
来自(irb):2
from/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in'start'
from/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in'start'
from/Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in`'
来自脚本/rails:6:in'require'
来自脚本/rails:6:in`'
我做错了什么?

这是对类方法的调用:

这是实例方法的定义:

您希望
constructKDTree
成为类方法,因此需要这样说:

def self.constructKDTree
  #...

您在清单中将
constructKDTree
定义为一个实例方法。因此,该方法仅适用于类的实例,而不适用于类本身

根据您实际想要实现的内容,您可以像在下面的代码中那样将该方法设置为类方法,或者您可以创建
列表
类的新实例,并在该实例上调用该方法

class Listing < ActiveRecord::Base
  def self.constructKDTree
    @contents = self.all
    @kdTree = KDTree.new(@contents) 
  end
end

嗨,谢谢。我来自Java背景,学习Ruby。那么类级方法和Java中的静态方法一样吗?@banditKing:是的,这是最接近的了。
NoMethodError: undefined method `constructKDTree' for #<Class:0x00000104b1f760>
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
from /Users/AM/Documents/RailsWS/cmdLineWS/Businesses/app/controllers/listings_controller.rb:20:in `index'
from (irb):2
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from /Users/AM/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
@tree = Listing.constructKDTree
def constructKDTree
  @contents = self.all
  @kdTree = KDTree.new(@contents) 
end
def self.constructKDTree
  #...
class Listing < ActiveRecord::Base
  def self.constructKDTree
    @contents = self.all
    @kdTree = KDTree.new(@contents) 
  end
end
listing = Listing.new
@tree = listing.constructKDTree