从zip存档加载ruby源? [英] Loading ruby source from a zip archive?

查看:157
本文介绍了从zip存档加载ruby源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mod_rails服务器,磁盘空间,奇怪的是,是一个溢价。有没有办法压缩我的应用程序的来源,如 Python的zipimport

I have a mod_rails server where disk space, oddly enough, is at a premium. Is there a way for me to compress my application's source, like Python's zipimport?

这有明显的缺点,所以我应该可以分解并在磁盘空间使用镍,但我认为这是值得一试。

There are obvious disadvantages to this, so I should probably just break down and spend a nickel on disk space, but I figured it'd be worth a shot.

推荐答案

哦,这很整洁。查看 ruby​​zip 宝石:

Oh, this is neat. Check out the rubyzip gem:


ruby​​zip还提供了
zip / ziprequire.rb 模块(),允许
ruby​​从zip
档案载入ruby模组。

rubyzip also features the zip/ziprequire.rb module (source) which allows ruby to load ruby modules from zip archives.

更新 ziprequire.rb不再存在于rubyzip gem中,但来源链接似乎包含其旧内容。)

(Update: The ziprequire.rb is no longer present in the rubyzip gem, but the source link appears to contain its old content nonetheless.)

这样。这只是稍微修改从他们的例子:

Like so. This is just slightly modified from their example:

require 'rubygems'
require 'zip/zipfilesystem'
require 'zip/ziprequire'

Zip::ZipFile.open("/tmp/mylib.zip", true) do |zip|
  zip.file.open('mylib/somefile.rb', 'w') do |file|
    file.puts "def foo"
    file.puts "  puts 'foo was here'"
    file.puts "end"
  end
end

$:.unshift '/tmp/mylib.zip'
require 'mylib/somefile'

foo    # => foo was here

您不必使用rubyzip库来创建压缩库。您可以使用CLI zip。

You don't have to use the rubyzip library to create the zipped library, of course. You can use CLI zip for that.

这篇关于从zip存档加载ruby源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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