在 proc 中设置全局 [英] setting a global within a proc

查看:45
本文介绍了在 proc 中设置全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力更好地理解 Ruby,但我遇到了一些问题:

I've been working on trying to better understand Ruby and here's something I'm having trouble with:

$SAFE = 1
puts $SAFE # 1  
proc {
  $SAFE = 2
  puts $SAFE  # 2
}.call
puts $SAFE # 1   

以上代码部分取自 eRB 的源代码,重写以更好地突出示例.基本上在 proc 中,可以将 $SAFE 的值设置为任何想要的值,并且在 proc 之后,SAFE 的值返回到它在 proc 之前的值.

The above code is partially taken from eRB's source rewritten to better highlight the example. Basically within the proc one can set the value of $SAFE to whatever value one wants and after the proc, the value of SAFE returns back to what it was before the proc.

如果我不使用 $SAFE 这个词,而是将它改成一个不同的词,例如 $DOOR:

If instead of using the word $SAFE I change it to a different word, such as $DOOR:

$DOOR = 1
puts $DOOR 
proc {
  $DOOR = 2
  puts $DOOR  
}.call
puts $DOOR  

那么proc后$DOOR的值是2而不是1.为什么这两个例子有区别?

then the value of $DOOR after the proc is 2 and not 1. Why the difference between the two examples?

推荐答案

其实很简单:$SAFE 的行为不像您对全局变量所期望的那样的原因是因为它不是全局变量.这是一只神奇的独角兽thingamajiggy.

It's rather simple, really: the reason why $SAFE doesn't behave like you would expect from a global variable is because it isn't a global variable. It's a magic unicorn thingamajiggy.

Ruby 中有不少神奇的独角兽 thingamajiggies,但遗憾的是它们没有很好的文档记录(实际上根本没有文档记录),因为替代 Ruby 实现的开发人员发现了困难的方法.这些 thingamajiggies 的行为都不同且(似乎)不一致,而且它们几乎唯一的两个共同点是它们看起来像全局变量,但行为却不像它们.

There are quite a few of those magic unicorn thingamajiggies in Ruby, and they are unfortunately not very well documented (not at all documented, in fact), as the developers of the alternative Ruby implementations found out the hard way. These thingamajiggies all behave differently and (seemingly) inconsistently, and pretty much the only two things they have in common is that they look like global variables but don't behave like them.

有些具有本地范围.有些具有线程局部作用域.有些神奇地改变,而没有任何人分配给他们.有些对解释器具有神奇的意义并改变了语言的行为方式.有些还附加了其他奇怪的语义.

Some have local scope. Some have thread-local scope. Some magically change without anyone ever assigning to them. Some have magic meaning for the interpreter and change how the language behaves. Some have other weird semantics attached to them.

$SAFE 几乎具有上述所有内容:它是线程本地的,这意味着如果您在一个线程中更改它,则不会影响其他线程.它是本地的,这意味着如果您在本地范围(如类、模块、方法或块)中更改它,它不会影响外部范围(如您所见).它对解释器具有神奇的意义,因为将其设置为不同于 0 的值会使某些事情不起作用.它还有其他奇怪的语义,因为你只能增加它的价值,永远不能减少它.

$SAFE has almost all of the above: it is thread-local, meaning that if you change it in one thread, it doesn't affect other threads. It is local, meaning if you change it in a local scope (like a class, module, method or block), it doesn't affect the outer scope (as you have discovered). It has magic meaning for the interpreter, since setting it to a value different than 0 makes certain things not work. And it has additional weird semantics in that you can only ever increase its value, never decrease it.

这篇关于在 proc 中设置全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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