从 zip 存档加载 ruby​​ 源代码?

发布于 2024-08-17 13:31:19 字数 217 浏览 6 评论 0原文

我有一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会傲 2024-08-24 13:31:19

哦,这很整洁。查看 rubyzip gem:

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

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

就像这样。这只是对他们的示例稍作修改:

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

当然,您不必使用 ruby​​zip 库来创建压缩库。您可以使用 CLI zip 来实现这一点。

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.

眼眸 2024-08-24 13:31:19

requireload 只是与其他方法一样的方法。您可以取消定义它们、重新定义它们、覆盖它们、挂钩它们、包装它们以执行您想要的任何操作。事实上,这正是 RubyGems 的工作原理。

现在,我不知道是否有人已经为您实现了这个,但我实际上记得 ruby-talk 邮件列表。

但是,有一些从替代位置加载库代码的示例可供您查看,并且可以复制/调整它们正在为您的目的所做的事情:

  • http_require 的作用与听起来差不多:它允许您 需要 HTTP URI
  • Crate 是一个 Ruby 打包工具,它将 Ruby 应用程序打包到一个二进制文件和几个 SQLite 数据库中;它修改 require 以从(加密的)SQLite 数据库而不是文件系统加载库
  • ,当然我已经提到了 红宝石宝石

require and load are just methods like any other. You can undefine them, redefine them, override them, hook them, wrap them to do anything you want. In fact, that's exactly how RubyGems works.

Now, I don't know whether someone has already implemented this for you, but I actually remember some discussions about this on the ruby-talk mailinglist.

However, there are some examples of loading library code from alternative locations that you can look at, and maybe copy / adapt what they are doing for your purpose:

  • http_require does pretty much what it sounds like: it allows you to require an HTTP URI
  • Crate is a Ruby packaging tool which packages a Ruby application into a single binary and a couple of SQLite databases; it modifies require to load libraries out of an (encrypted) SQLite database instead of the filesystem
  • and of course I already mentioned RubyGems
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文