当前位置:  开发笔记 > 后端 > 正文

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

如何解决《如何将rubygem添加到"require"的include路径中》经验,为你挑选了1个好方法。

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

谢谢



1> rampion..:

实际上并不难以手动执行此操作.假设您有一个whatever.rb要作为gem分发的库.

    建立一个目录lib,并把副本whatever.rblib/whatever.rb.

    制作一个文件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

    现在,要构建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
    %
    

    您可以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
    

有关gemspec文件中各种设置的更多信息,请查看GemSpec参考.

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

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有