如何将ruby gem添加到“require”的include路径中

时间:2009-03-26 06:39:45

标签: ruby rubygems

我正在创造我期望的红宝石宝石。任何人都有一个很好的链接指向将简单的库或插件转换为gem的教程?另外,特别是,Ruby使用什么来允许需要查找宝石?它似乎不仅仅是将文件放在gem路径中(或者我的配置搞砸了?)。

由于

3 个答案:

答案 0 :(得分:26)

实际上并不难以手动执行此操作。假设您有一个库whatever.rb,您希望将其作为gem分发。

  1. 制作目录lib并在whatever.rb中放置lib/whatever.rb的副本。
  2. 制作文件whatever.gemspec,并在其中填写以下内容,填写相应的值:
    
     Gem::Specification.new do |spec|
       spec.name = 'the-name-of-your-gem'
       spec.version ='0.0.1'
    # this is important - it specifies which files to include in the gem. spec.files = ["lib/whatever.rb"]
    # optional, but useful to your users spec.summary = "A more longwinded description of your gem" spec.author = 'Your Name' spec.email = 'you@yourdomain.com' spec.homepage = 'http://www.yourpage.com'
    # you did document with RDoc, right? spec.has_rdoc = true
    # if you have a ruby forge project spec.rubyforge_project = 'your-project-name-on-rubyforge'
    # if you have any dependencies on other gems, list them thusly spec.add_dependency('hpricot') spec.add_dependency('log4r', '>= 1.0.5') end
  3. 现在,要构建gem,请使用gem build命令:
    % gem build whatever.gemspec
    Successfully built RubyGem
    Name: the-name-of-your-gem
    Version: 0.0.1
    File: the-name-of-your-gem-0.0.1.gem
    %
    
  4. 您可以使用gem install the-name-of-your-gem-0.0.1.gem进行本地测试 要在脚本中使用您的库,只需在顶部执行以下操作:
    
    require 'rubygems' # puts gem libraries in the require path
    require 'whatever' # loads your library
    
  5. 有关gemspec文件中各种设置的详情,请查看GemSpec Reference

    就个人而言,我也使用rubygems来打包可执行脚本,并且发现它非常方便。

答案 1 :(得分:2)

来自Nic博士的oldish one here使用了newgem。现在可能有点过时了。

最近,我们得到了gemify,这似乎建立在上面。 Here's a post about that

就路径而言,一旦你执行gem install <some-useful-stuff>并且你的要求代码已执行require 'rubygems'或你设置了RUBYOPT = -rubygems(仅限Windows?我不是确定)然后require的后续执行也将在gems目录中搜索适当的库。

还有更明确​​(但已弃用)的require_gem命令,它现在只是普通的gem,它允许您指定版本号。

答案 2 :(得分:2)

要制作红宝石宝石,您可能需要查看newgemjeweler