有没有办法在不加载rubygems的情况下调用ruby1.9?

时间:2022-12-14 07:14:46

So ruby 1.9 is really nice in that it'll automatically require rubygems, and hence when you call require 'somegem' without first requiring rubygems it'll work, and that's generally awesome.

因此ruby 1.9非常好,因为它会自动需要rubygems,因此当你需要'somegem'而不首先需要rubygems时它会工作,而且这通常很棒。

But I have a ton of shell scripts using ruby, and they generally don't rely on rubygems. Shell tools should run instantly, and loading rubygems for nothing is a major drag, mostly because it involves a bunch of disk operations with scattered small files.

但是我有大量使用ruby的shell脚本,而且它们通常不依赖于rubygems。 Shell工具应该立即运行,并且无需加载rubygems是一个主要障碍,主要是因为它涉及一堆磁盘操作和分散的小文件。

I want to be able to tell ruby, when running these shell scripts, to skip loading gems. Ideally, something like #!ruby --no-rubygems in the shebang line.

我希望能够在运行这些shell脚本时告诉ruby跳过加载gem。理想情况下,像shebang一样的#!ruby --no-rubygems。

Is there such a thing? Or maybe a compile option that'll tell ruby rubygems must be required manually?

有这样的事吗?或者可能是一个编译选项,它将告诉ruby ruby​​gems必须手动需要?

1 个解决方案

#1


5  

Yes, you can use the --disable-gems option.

是的,您可以使用--disable-gems选项。

Note that whether or not passing options in the shebang line works depends on your operating system. Some operating systems don't support passing options at all, some only support passing one option or argument.

请注意,在shebang行中传递选项是否有效取决于您的操作系统。有些操作系统根本不支持传递选项,有些只支持传递一个选项或参数。

So, if you have for example

所以,如果你有例子

#!/usr/bin/env ruby

Then it's pretty unlikely that you will be able to attach the option to the end. If OTOH you change that to

那么你几乎不可能将选项附加到最后。如果OTOH你改变它

#!/usr/local/bin/ruby --disable-gems

Then you have hardwired the location of the Ruby binary into your script.

然后,您已将Ruby二进制文件的位置硬连线到您的脚本中。

And of course there are operating systems that don't interpret shebang lines at all. (After all, they were never specified in any standard, and aren't even properly documented.)

当然,有些操作系统根本不解释shebang线路。 (毕竟,它们从未在任何标准中指定,甚至没有正确记录。)

An alternative would be to set the RUBYOPT environment variable in your shell environment and simply switch to a different environment with RUBYOPT unset (or set to -w, my personal favorite) for your Ruby development.

另一种方法是在shell环境中设置RUBYOPT环境变量,并简单地切换到RUBYOPT未设置(或设置为-w,我个人最喜欢的)用于Ruby开发的不同环境。

#1


5  

Yes, you can use the --disable-gems option.

是的,您可以使用--disable-gems选项。

Note that whether or not passing options in the shebang line works depends on your operating system. Some operating systems don't support passing options at all, some only support passing one option or argument.

请注意,在shebang行中传递选项是否有效取决于您的操作系统。有些操作系统根本不支持传递选项,有些只支持传递一个选项或参数。

So, if you have for example

所以,如果你有例子

#!/usr/bin/env ruby

Then it's pretty unlikely that you will be able to attach the option to the end. If OTOH you change that to

那么你几乎不可能将选项附加到最后。如果OTOH你改变它

#!/usr/local/bin/ruby --disable-gems

Then you have hardwired the location of the Ruby binary into your script.

然后,您已将Ruby二进制文件的位置硬连线到您的脚本中。

And of course there are operating systems that don't interpret shebang lines at all. (After all, they were never specified in any standard, and aren't even properly documented.)

当然,有些操作系统根本不解释shebang线路。 (毕竟,它们从未在任何标准中指定,甚至没有正确记录。)

An alternative would be to set the RUBYOPT environment variable in your shell environment and simply switch to a different environment with RUBYOPT unset (or set to -w, my personal favorite) for your Ruby development.

另一种方法是在shell环境中设置RUBYOPT环境变量,并简单地切换到RUBYOPT未设置(或设置为-w,我个人最喜欢的)用于Ruby开发的不同环境。