简体   繁体   English

如何在Rails应用程序中使用RubyGems类

[英]How to use a RubyGems class in a Rails app

I need to stream a TAR file using data from the database in a Rails app. 我需要使用Rails应用程序中的数据库中的数据来流式传输TAR文件。 I know that RubyGems has the TarWriter module that suits my use case perfectly. 我知道RubyGems有一个完全适合我的用例的TarWriter模块。

The question is, how can I include the module in my Rails app? 问题是,如何在我的Rails应用程序中包含该模块? I tried to require rubygems/package as follows: 我试图要求rubygems / package如下:

require 'zlib'
require 'rubygems/package'

tar = StringIO.new

Gem::Package::TarWriter.new(tar) do |writer|
    writer.add_file("a_file.txt", 0644) do |f| 
        (1..1000).each do |i| 
            f.write("some text\n")
        end
    end
    writer.add_file("another_file.txt", 0644) do |f| 
        f.write("some more text\n")
    end
end
tar.seek(0)

gz = Zlib::GzipWriter.new(File.new('this_is_a_tar_gz.tar.gz', 'wb'))
gz.write(tar.read)
tar.close
gz.close

The require failed on the controller. 控制器上的require失败。 I tried to adding gem 'rubygems/package' on my Gemfile but it didn't work. 我试图在我的Gemfile上添加Gemfile gem 'rubygems/package' ,但它没有用。

It's a noob question, how can I use RubyGems modules from a Rails app? 这是一个noob问题,我如何使用Rails应用程序中的RubyGems模块?

您应该指定模块的完整路径: Gem::Package::TarWriter并向控制器添加Gem::Package::TarWriter require 'rubygems/package'

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

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