在irb会话中需要任意ruby gem的ruby

在irb会话中需要任意ruby gem的ruby,ruby,Ruby,我一直在玩Rails有一段时间了。但现在我正试图构建一个ruby宝石。我正在使用rubymine为您构建一个gem模板。在我的例子中,它看起来是这样的: $ ls bin Gemfile lib Rakefile test binarytree.gemspec Gemfile.lock LICENSE.txt README.md merlino@johnmerlino:~/Documents/github/binarytre

我一直在玩Rails有一段时间了。但现在我正试图构建一个ruby宝石。我正在使用rubymine为您构建一个gem模板。在我的例子中,它看起来是这样的:

$ ls
bin                 Gemfile       lib          Rakefile   test
binarytree.gemspec  Gemfile.lock  LICENSE.txt  README.md
merlino@johnmerlino:~/Documents/github/binarytree$ 
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
在lib目录中,我有一个名为binarytree.rb的文件,其中包含以下内容:

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
我想做的是运行一个irb交互式ruby shell会话,然后能够要求“binarytree”,并将此代码放在irb的范围内,这样我就可以开始使用它了,例如binarytree.new

目前,我不确定如何在irb中要求:

需要“二进制树” LoadError:无法加载此类文件-binarytree from/home/merlino/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site\u ruby/2.0.0/rubygems/core\u ext/kernel\u require.rb:45:in require' from/home/merlino/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site\u ruby/2.0.0/rubygems/core\u ext/kernel\u require.rb:45:inrequire' 来自irb:1 from/home/merlino/.rvm/rubies/ruby-2.0.0-p0/bin/irb:13:in`'

我在Ubuntu上使用rvm管理gems

有什么想法吗?

您有两个选择:

进入您的gem目录并运行require'/lib/binarytree.rb' 在您的gem目录中运行rake安装-这将构建并安装这个gem到系统gems中。
我通过以下方式使其工作:

1您首先需要编辑gemspec:

binarytree.gemspec
并编辑描述和摘要行,如下所示:

  spec.description   = "binary tree"
  spec.summary       = "binary tree summary"
否则将出现以下错误:

 gem build doctor_toons.gemspec
ERROR:  While executing gem ... (Gem::InvalidSpecificationException)
    "FIXME" or "TODO" is not a description
2然后运行gempec,如下所示:

gem build binarytree.gemspec
这将输出如下所示的内容:

$ ls
bin                 Gemfile       lib          Rakefile   test
binarytree.gemspec  Gemfile.lock  LICENSE.txt  README.md
merlino@johnmerlino:~/Documents/github/binarytree$ 
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
二进制树-0.0.1.gem

3现在,如果您正在使用rvm,请确保您正在使用所需的版本,并运行以下程序:

gem install ./binarytree-0.0.1.gem 
输出应如下所示:

$ ls
bin                 Gemfile       lib          Rakefile   test
binarytree.gemspec  Gemfile.lock  LICENSE.txt  README.md
merlino@johnmerlino:~/Documents/github/binarytree$ 
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然后启动irb并要求新的gem:

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