红宝石在独立的红宝石脚本 [英] Ruby gems in stand-alone ruby scripts

查看:133
本文介绍了红宝石在独立的红宝石脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的红宝石宝石问题。我熟悉编写如下简单的ruby脚本:

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

现在我'我想在我的剧本中使用我自己的红宝石宝石。在我的rails项目中,我可以简单地 require'my_gem'。但是,这不适用于独立脚本。 在独立的ruby脚本中使用我自己的gem的最佳方式是什么?

>您应该可以直接在最新版本的Ruby中直接使用它。

 #可选,还允许指定版本
gem'chronic','〜> 0.6'

#只需要并使用它
需要'慢'
将Chronic :: VERSION#放到0.6。 7for me

如果您仍然使用Ruby 1.8(默认情况下不需要RubyGems),您必须明确地将此行放在您尝试加载gem的上方:

  require'rubygems'

code>

或者,您可以调用标记为 -rubygems 的Ruby解释器会有相同的效果。



另见:


This is a really basic ruby gems question. I'm familiar with writing simple ruby scripts like this:

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

Now I'd like to use my own ruby gem in my script. In my rails project I can simply 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?

解决方案

You should be able to simply require it directly in recent versions of 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:

require 'rubygems'

Alternatively, you can invoke the Ruby interpreter with the flag -rubygems which will have the same effect.

See also:

这篇关于红宝石在独立的红宝石脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆