gpt4 book ai didi

ruby - 如何将 ruby​​ gem 放入 "require"的包含路径

转载 作者:数据小太阳 更新时间:2023-10-29 06:40:24 26 4
gpt4 key购买 nike

我正在创建我期望成为 ruby 的东西。任何人都有关于将简单库或插件转换为 gem 的教程的良好链接?另外,特别是,Ruby 允许 require 找到 gems 的过程是什么?这似乎不仅仅是将文件放在 gem 路径中(或者我的配置搞砸了?)。

谢谢

最佳答案

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

  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'<br/>
    # this is important - it specifies which files to include in the gem.
    spec.files = ["lib/whatever.rb"]<br/>
    # 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 = '<a href="http://www.yourpage.com" rel="noreferrer noopener nofollow">http://www.yourpage.com</a>'<br/>
    # you did document with RDoc, right?
    spec.has_rdoc = true<br/>
    # if you have a ruby forge project
    spec.rubyforge_project = 'your-project-name-on-rubyforge'<br/>
    # 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.gemspecSuccessfully built RubyGemName: the-name-of-your-gemVersion: 0.0.1File: the-name-of-your-gem-0.0.1.gem%
  4. You can test locally by using gem install the-name-of-your-gem-0.0.1.gemTo use your library in a script then, simply do the following at the top:

    require 'rubygems' # puts gem libraries in the require path
    require 'whatever' # loads your library

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

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

关于ruby - 如何将 ruby​​ gem 放入 "require"的包含路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/684737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com