简体   繁体   English

独立的ruby脚本中的Ruby gem

[英]Ruby gems in stand-alone ruby scripts

This is a really basic ruby gems question. 这是一个非常基本的红宝石宝石问题。 I'm familiar with writing simple ruby scripts like this: 我很熟悉编写这样的简单ruby脚本:

#!/usr/bin/ruby
require 'time'
t = Time.at(123)
puts t

Now I'd like to use my own ruby gem in my script. 现在我想在我的脚本中使用我自己的ruby gem。 In my rails project I can simply require 'my_gem' . 在我的rails项目中,我可以简单地require 'my_gem' However this doesn't work in a stand-alone script. 但是,这在独立脚本中不起作用。 What's the best/proper way to use my own gem in a stand-alone ruby script? 在独立的ruby脚本中使用我自己的gem的最佳/正确方法是什么?

You should be able to simply require it directly in recent versions of Ruby. 您应该能够直接在最新版本的Ruby中直接使用它。

# optional, also allows you to specify version
gem 'chronic', '~>0.6'

# just require and use it
require 'chronic'
puts Chronic::VERSION  # yields "0.6.7" for me

If you are still on Ruby 1.8 (which does not require RubyGems by default), you will have to explicitly put this line above your attempt to load the gem: 如果您仍然使用Ruby 1.8(默认情况下不需要RubyGems),则必须明确将此行放在加载gem的尝试之上:

require 'rubygems'

Alternatively, you can invoke the Ruby interpreter with the flag -rubygems which will have the same effect. 或者,您可以使用标志-rubygems调用Ruby解释器,这将具有相同的效果。

See also: 也可以看看:

You could use something like this. 你可以使用这样的东西。 It will install the gem if it's not already installed: 如果它尚未安装,它将安装gem:

def load_gem(name, version=nil)
  # needed if your ruby version is less than 1.9
  require 'rubygems'

  begin
    gem name, version
  rescue LoadError
    version = "--version '#{version}'" unless version.nil?
    system("gem install #{name} #{version}")
    Gem.clear_paths
    retry
  end

  require name
end

load_gem 'your_gem'

Installing gems with something like the following should work. 安装具有以下内容的宝石应该可行。 Be mindful of whether gems should be installed as part of system ruby or a user's. 请注意宝石是应该作为系统ruby还是用户的一部分安装。

#!/usr/bin/env ruby

require 'rubygems'

def install_gem(name, version=Gem::Requirement.default)
  begin
    gem name, version
  rescue LoadError
    print "ruby gem '#{name}' not found, " <<
      "would you like to install it (y/N)? : "
    answer = gets
    if answer[0].downcase.include? "y"
      Gem.install name, version
    else
      exit(1)
    end
  end
end

# any of the following will work...
install_gem 'activesupport'
install_gem 'activesupport', '= 4.2.5'
install_gem 'activesupport', '~> 4.2.5'

# require as normal (since not all gems install & require with same name) ...
require 'active_support/all'

...

I'm not sure if I understood your question right, but perhaps you don't have a gem, even if you write it (you are a beginner, so perhaps you misunderstood the concept of gems). 我不确定我是否理解你的问题,但也许你没有宝石,即使你写它(你是初学者,所以也许你误解了宝石的概念)。

Just to be sure: You have a gemspec for your gem? 只是为了确定:你有宝石的gemspec? If not, then you have no gem, but a single script. 如果没有,那么你没有宝石,只有一个脚本。

When you want your own script inside another script, you may just do: 如果您希望自己的脚本在另一个脚本中,您可以这样做:

require 'my_script'

With ruby 1.8 this works fine, if my_script.rb is in the same folder as your main script. 使用ruby 1.8,如果my_script.rb与主脚本位于同一文件夹中,则可以正常工作。 With ruby 1.9+ you can use: 使用ruby 1.9+,您可以使用:

require_relative 'my_script'

There is no need of a gem in this case. 在这种情况下不需要宝石。

It is to be noted that bundler itself can deal with this. 需要注意的是, bundler本身可以处理这个问题。 It's particularly interesting since bundler ships with Ruby by default since version 2.6, and you don't need to install it manually anymore. 它特别有趣,因为bundler默认从2.6版开始附带Ruby,你不需要再手动安装它。

The idea is: 这个想法是:

  1. to require bundler/inline at the top of your script, 要求在脚本顶部使用bundler/inline
  2. to use the gemfile method, and declare the gems you need inside a block, like you'd do in a Gemfile , 使用gemfile方法,并在块中声明所需的gem,就像在Gemfile
  3. after the end of this section, your gems are available! 在本节结束后,您的宝石可用!

For instance: 例如:

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'rainbow'
end

# From here on, rainbow is available so I can 
# print colored text into my terminal

require 'rainbow'
puts Rainbow('This will be printed in red').red

The official documentation can be found on bundler website: bundler in a single file ruby script 官方文档可以在bundler网站找到:bundler在单个文件ruby脚本中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM