1
votes

I've been playing around with Rails for some time. But now I am attempting to build a ruby gem. And I am using rubymine which builds a gem template for you. In my case it looks like this:

$ ls
bin                 Gemfile       lib          Rakefile   test
binarytree.gemspec  Gemfile.lock  LICENSE.txt  README.md
merlino@johnmerlino:~/Documents/github/binarytree$ 

Inside the lib directory, I have a file called binarytree.rb, which contains the following contents:

require "binarytree/version"

module Binarytree

  class BinaryNode

    attr_accessor :value, :left, :right

    def initialize(value=nil)
      @value = value
      @left = nil
      @right = nil
    end

    def add(value)
      if value <= @value
        if @left
          @left.add value
        else
          @left = BinaryNode.new value
        end
      else
        if @right
          @right.add value
        else
          @right = BinaryNode.new value
        end
      end
    end
  end

  class BinaryTree

    attr_accessor :root

    def initialize
      @root = nil
    end

    def add(value)
      if !@root
        @root = BinaryNode.new value
      else
        @root.add value
      end
    end

    def contains(value)
      node = @root
      while node
        if value == node.value
          return true
        elsif value < node.value
          node = node.left
        else
          node = node.right
        end
      end
      false
    end
  end
end

What I want to be able to do is run an irb (interactive ruby shell) session, and then be able to require 'binarytree' and have this code inside scope of irb, so I could start playing with it e.g. BinaryTree.new.

Right now I am not sure how to require this in irb:

require 'binarytree' LoadError: cannot load such file -- binarytree from /home/merlino/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in require' from /home/merlino/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:inrequire' from (irb):1 from /home/merlino/.rvm/rubies/ruby-2.0.0-p0/bin/irb:13:in `'

I am on Ubuntu and I am using rvm to manage gems.

Any ideas?

2

2 Answers

0
votes

You have two options:

  1. Go into catalog of your gem and run require './lib/binarytree.rb'
  2. Run rake install inside catalog of your gem - this will build and install this gem into system gems.
0
votes

I got it working the following way:

1) You first need to edit the gemspec:

binarytree.gemspec

And edit the description and summary lines as so:

  spec.description   = "binary tree"
  spec.summary       = "binary tree summary"

Otherwise you will get the following error:

 gem build doctor_toons.gemspec
ERROR:  While executing gem ... (Gem::InvalidSpecificationException)
    "FIXME" or "TODO" is not a description

2) Then run the gemspec as so:

gem build binarytree.gemspec

This should output something that looks like this:

binarytree-0.0.1.gem

3) Now if you are using rvm, make sure you are using the version you want, and run the following:

gem install ./binarytree-0.0.1.gem 

The output should look something like this:

Successfully installed binarytree-0.0.1
Parsing documentation for binarytree-0.0.1
Installing ri documentation for binarytree-0.0.1
Done installing documentation for binarytree after 0 seconds
Done installing documentation for binarytree (0 sec).
1 gem installed

4) Then launch irb and require the new gem:

irb(main):001:0> require 'binarytree'