Ruby 不能';我不理解命令行选项是如何-";在实践中使用

Ruby 不能';我不理解命令行选项是如何-";在实践中使用,ruby,ruby-1.9.3,Ruby,Ruby 1.9.3,无法理解下面的命令行选项在实践中是如何使用的 -T[level=1] 我尝试了以下代码: #commandoptionstest.rb puts "hello world" 具有各种安全级别: 输出正常 @ubuntu:~/script$ ruby -x commandoptionstest.rb # => hello world 为什么会出错?我需要在命令选项test.rb中执行什么操作才能允许-x使用-T @ubuntu:~/script$ ruby -x -T commando

无法理解下面的命令行选项在实践中是如何使用的

-T[level=1]
我尝试了以下代码:

#commandoptionstest.rb
puts "hello world"
具有各种
安全
级别:

输出正常

@ubuntu:~/script$ ruby -x commandoptionstest.rb
# => hello world
为什么会出错?我需要在
命令选项test.rb中执行什么操作才能允许
-x
使用
-T

@ubuntu:~/script$ ruby -x -T commandoptionstest.rb
# => ruby: no -x allowed in tainted mode (SecurityError)
产出即将到来

@ubuntu:~/script$ ruby -T commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T1 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T2 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T3 commandoptionstest.rb
# => hello world
产出即将到来

@ubuntu:~/script$ ruby -T commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T1 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T2 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T3 commandoptionstest.rb
# => hello world
产出即将到来

@ubuntu:~/script$ ruby -T commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T1 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T2 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T3 commandoptionstest.rb
# => hello world
产出即将到来

@ubuntu:~/script$ ruby -T commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T1 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T2 commandoptionstest.rb
# => hello world
@ubuntu:~/script$ ruby -T3 commandoptionstest.rb
# => hello world
为什么会再次出错

@ubuntu:~/script$ ruby -T4 commandoptionstest.rb
# => commandoptionstest.rb:15:in `write': Insecure operation `write' at level 4 (SecurityError)
#   from commandoptionstest.rb:15:in `puts'
#   from commandoptionstest.rb:15:in `puts'
#   from commandoptionstest.rb:15:in `<main>'
@ubuntu:~/script$ruby-T4 commandoptiontest.rb
#=>CommandOptionTest.rb:15:in'write':在级别4上的不安全操作'write'(SecurityError)
#来自commandoptiontest.rb:15:in'puts'
#来自commandoptiontest.rb:15:in'puts'
#从commandoptiontest.rb:15:in`'

借助上述代码,您能否解释一下为什么
安全
级别
1
2
3
正在打印
“hello world”
,而
安全
级别
4
没有打印?要允许在
SAFE
级别
4
执行写操作,这里应该做什么?

它设置
$SAFE
级别

这说明了如何处理输入,以及与环境变量、I/O、线程、异常、解释器命令行参数等有关的大量其他内容

在我看来,这些文件是一个很好的开始。如果你对某一特定行为有疑问,可以提问


要处理您的评论和编辑,请执行以下操作:

是的,我可以,但是文档也可以,而且可能更好

为什么
-x
不起作用?

因为医生说不会:

$SAFE>=1
*不允许使用命令行选项-e、-i、-r、-s、-s和-x

[~]$ruby——帮助 用法:ruby[switches][--][programfile][arguments] #省略 -T[level=1]打开污染检查

因此,如果
-T
未指定任何数字,则默认级别为
1
,这意味着
$SAFE>=1
,这正是文档所说的:
-x
是不允许的

为什么
不让
工作?

很难说,因为您实际上没有提供正在执行的代码,但很可能,正如文档所说:

$SAFE>=4
*无法写入文件或管道


比我解释的好得多。基本上,这是一种确保您的参数已由您验证的方法。

您没有看您复制的问题吗?@hd1 IMO这不是一个复制,因为
-T
影响的不仅仅是对象污染。请停止投票,以作为复制关闭:不是。链接的问题专门询问污染对象。
-T
命令行选项设置Ruby解释器的
$SAFE
级别,它不仅仅是简单地定义对象是否被污染、被污染以及何时被污染。不过,对于Ruby来说,它只是部分有用,因为发生的很多事情都是Ruby和安全级别特定的,nevertheless@DaveNewton我同意你的看法。我的问题并没有完全回答他标记的问题。@hd1我不同意,因为这个问题所问的不仅仅是对象污染,它问的是受
-T
(即
$SAFE
)影响的行为,这涵盖了更多的领域。然后我显然误解了他的问题的范围,很抱歉,可以用IRB类型的小片段显示,看看什么级别会影响代码中使用
-T
选项的方式?@user2060534一个详尽的示例可能有数百行——我认为完整的答案不在范围之内。请查看提供的文档,并将您的问题缩小到具体的行为——要点列表包括三十多个具体的行为。实际上,让我困惑的是——我想看看——如果我不将
SAFE
level定义为
2
——那么会发生什么,如果定义了,那么它将如何保证安全?@user2060534。。。我链接到的文档中描述了这些行为。解释器处理各种
$SAFE
级别。实际上,我在控制台中尝试了命令行的其他选项,并了解它们的工作原理,这是我无法通过这种方式测试的选项:(