Charlie Davies
Charlie Davies

Reputation: 1834

'require' gem error in Jruby

I am running Jruby 1.6.7 in ruby 1.8

I can install the gem: https://github.com/consti/tupalo-kdtree.

I have the code below:

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

However when I run this in Jruby - I get the error

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

Its odd, as it is saying it can't even find kdtree - when I know it is there - the gem installed. I have cleaned out the gemset and created a new one under RVM but still the same error. Is there a different way of 'require' under jruby? Or is there something else wrong?

Upvotes: 0

Views: 855

Answers (1)

Robert Brown
Robert Brown

Reputation: 11016

Add this to the top

require 'rubygems'

Upvotes: 1

Related Questions