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

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

问题描述

我有一个 mod_rails 服务器,其中的磁盘空间很奇怪,非常宝贵.有没有办法压缩我的应用程序的源代码,比如 Python 的 zipimport?>

这样做有明显的缺点,所以我可能应该分解并在磁盘空间上花一点钱,但我认为值得一试.

解决方案

哦,这很好.查看 rubyzip gem:

<块引用>

rubyzip 还具有zip/ziprequire.rb 模块(source) 允许ruby 从 zip 加载 ruby​​ 模块存档.

(更新:ziprequire.rb 不再存在于 ruby​​zip gem 中,但是 source 链接似乎包含其旧内容.)

像这样.这只是从他们的示例中稍作修改:

需要'rubygems'需要'zip/zipfilesystem'需要'zip/ziprequire'Zip::ZipFile.open("/tmp/mylib.zip", true) 做 |zip|zip.file.open('mylib/somefile.rb', 'w') 做 |file|file.puts "def foo"file.puts " puts 'foo was here'"file.puts结束"结尾结尾$:.unshift '/tmp/mylib.zip'需要'mylib/somefile'foo # =>foo 在这里

当然,您不必使用 ruby​​zip 库来创建压缩库.您可以为此使用 CLI zip.

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.

解决方案

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

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

(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

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天全站免登陆