我在ruby 1.8中运行Jruby 1.6.7
我可以安装gem:https://github.com/consti/tupalo-kdtree。
我有以下代码:
require 'kdtree'
require 'benchmark'
def setup_tree(len)
@points = (0...len).map { |i| [rand_coord, rand_coord, i ] }
@kdtree = KDTree.new(@points)
end
def distance(a, b)
x, y = a[0] - b[0], a[1] - b[1]
x * x + y * y
end
def rand_coord
rand(0) * 10 - 5
end
def test_nearestk
pt = []
@list = []
20000.times do
pt = [rand_coord, rand_coord]
# kdtree search
@list << @kdtree.nearest(pt[0],pt[1]) #puts pt
end
puts "Points from search #{@list.size}"
puts "Points in graph #{@kdsize}"
end
@kdsize = 50000
setup_tree(@kdsize)
Benchmark.bm do |x|
x.report do
test_nearestk
end
end
然而,当我在Jruby中运行时 - 我收到错误
LoadError: no such file to load -- kdtree
require at org/jruby/RubyKernel.java:982
require at /home/charlie/.rvm/rubies/jruby-head/lib/ruby/shared/rubygems/custom_require.rb:36
(root) at newtest.rb:2
它奇怪,因为它说它甚至找不到kdtree - 当我知道它在那里 - 安装了宝石。我已经清理了gemset并在RVM下创建了一个新的但仍然是同样的错误。在jruby下有“不同”的“需要”方式吗?或者还有其他错误吗?
答案 0 :(得分:1)
将其添加到顶部
require 'rubygems'