ruby全局变量
Global Variables are variables that may be accessed from anywhere in the program regardless of scope. They're denoted by beginning with a $ (dollar sign) character. However, the use of global variables is often considered "un-Ruby," and you will rarely see them.
全局变量是可以从程序中的任何位置访问的变量 ,无论其范围如何。 它们以$(美元符号)字符开头。 但是,全局变量的使用通常被认为是“ un-Ruby”,并且您很少会看到它们。
定义全局变量 ( Defining Global Variables )
Global variables are defined and used like any other variable. To define them, simply assign a value to them and begin using them. But, as their name suggests, assigning to global variables from any point in the program has global implications. The following program demonstrates this. The method will modify a global variable, and that will affect how the second method runs.
全局变量的定义和使用与其他任何变量一样。 要定义它们,只需为它们分配一个值并开始使用它们。 但是,顾名思义,从程序的任何位置分配全局变量都具有全局含义。 下面的程序演示了这一点。 该方法将修改全局变量,这将影响第二种方法的运行方式。
$speed = 10
def accelerate
$speed = 100
end
def pass_speed_trap
if $speed > 65
# Give the program a speeding ticket
end
end
accelerate
pass_speed_trap
不受欢迎 ( Unpopular )
So why is this "un-Ruby" and why don't you see global variables very often? Put simply, it breaks encapsulation. If any one class or method can modify the state of the global variables at will with no interface layer, any other classes or methods that rely on that global variable may behave in an unexpected and undesirable manner. Further, such interactions can be very difficult to debug. What modified that global variable and when? You'll be looking through quite a lot of code to find what did it, and that could have been avoided by not breaking the rules of encapsulation.
那么,为什么这是“非Ruby”呢?为什么不经常看到全局变量呢? 简而言之,它破坏了封装。 如果任何一个类或方法都可以在没有接口层的情况下随意修改全局变量的状态,则任何依赖于该全局变量的其他类或方法都可能会以意外的方式出现不良行为。 此外,这种交互可能很难调试。 什么时候修改了全局变量? 您将浏览大量的代码以查找执行该操作的方法,而这可以通过不破坏封装规则来避免。
But that's not to say that global variables are never used in Ruby. There are a number of special global variables with single-character names (a-la Perl) that can be used throughout your program. They represent the state of the program itself, and do things like modify the record and field separators for all gets methods.
但这并不是说Ruby中从未使用过全局变量。 有许多带有单字符名称的特殊全局变量(a-la Perl ),可以在整个程序中使用。 它们表示程序本身的状态,并执行诸如修改所有get方法的记录和字段分隔符之类的操作。
全局变量 ( Global Variables )
$0 - This variable, denoted by $0 (that's a zero), holds the name of the top-level script being executed. In other words, the script file that was run from the command line, not the script file that holds the currently executing code. So, if script1.rb was run from the command line, it would hold script1.rb. If this script requires script2.rb, $0 in that script file would also be script1.rb. The name $0 mirrors the naming convention used in UNIX shell scripting for the same purpose.
$ 0-此变量用$ 0(即零)表示,它包含正在执行的顶级脚本的名称。 换句话说,这是从运行脚本文件的命令行 ,而不是保存当前正在执行的代码的脚本文件。 因此,如果从命令行运行script1.rb ,它将保存script1.rb 。 如果此脚本需要script2.rb ,则该脚本文件中的$ 0也将是script1.rb 。 名称$ 0反映了UNIX shell脚本中出于相同目的使用的命名约定。
$* - The command-line arguments in an array denoted by $* (dollar sign and asterisk). For example, if you were to run ./script.rb arg1 arg2, then $* would be equivalent to %w{ arg1 arg2 }. This is equivalent to the special ARGV array and has a less descriptive name, so it is rarely used.
$ * - $ *表示的数组中的命令行参数(美元符号和星号)。 例如,如果您要运行./script.rb arg1 arg2 ,则$ *将等效于%w {arg1 arg2} 。 这等效于特殊的ARGV数组,并且具有较少的描述性名称,因此很少使用。
$$ - The interpreter's process ID, denoted by $$ (two dollar signs). Knowing one's own process ID is often useful in daemon programs (which run in the background, unattached from any terminal) or system services. However, this gets a bit more complicated when threads are involved, so be wary of using it blindly.
$$ -解释程序的进程ID,以$$(两个美元符号)表示。 知道自己的进程ID在守护程序(在后台运行,与任何终端无关)中或系统服务中通常很有用。 但是,当涉及线程时,这会变得更加复杂,因此请谨慎使用它。
$/ and $\ - These are the input and output record separators. When you read objects using gets and print them using puts, it uses these to know when a complete "record" has been read, or what to print between multiple records. By default, these should be the newline character. But since these affect the behavior of all IO objects, they're rarely used, if at all. You may see them in smaller scripts where breaking the encapsulation rules is not an issue.
$ /和$ \ -这些是输入和输出记录分隔符。 当您使用gets读取对象并使用puts打印对象时,它将使用这些对象知道何时已读取完整的“记录”,或在多个记录之间打印什么内容。 默认情况下,这些应该是换行符。 但是由于这些会影响所有IO对象的行为,因此很少使用它们,即使有的话也很少使用。 您可能会在较小的脚本中看到它们,在这些脚本中,打破封装规则不是问题。
$? - The exit status of the last child process executed. Of all the variables listed here, this is probably the most useful. The reason for this is simple: you can't get the exit status of child processes by their return value from the system method, only true or false. If you must know the actual return value of the child process, you need to use this special global variable. Again, the name of this variable is taken from the UNIX shells.
$? -最后执行的子进程的退出状态。 在这里列出的所有变量中,这可能是最有用的。 原因很简单:您无法通过子方法从系统方法的返回值获得子过程的退出状态,只能是true或false。 如果必须知道子进程的实际返回值,则需要使用此特殊全局变量。 同样,此变量的名称取自UNIX Shell。
$_ - The last string read by gets. This variable may be a point of confusion for those coming to Ruby from Perl. In Perl, the $_ variable means something similar, but totally different. In Perl, $_ holds the value of the last statement and in Ruby it holds the string returned by the previous gets invocation. Their usage is similar, but what they really hold is very different. You don't often see this variable either (come to think of it, you rarely see any of these variables), but you may see them in very short Ruby programs that process text.
$ _ -最后的字符串阅读得到 。 对于从Perl进入Ruby的人们来说,这个变量可能是一个困惑点。 在Perl中, $ _变量的含义相似但完全不同。 在Perl中, $ _保存最后一条语句的值,在Ruby中,它保存先前的get调用返回的字符串。 它们的用法相似,但是它们的实际用途却大不相同。 您也不经常看到此变量(想起来,很少看到这些变量中的任何一个),但是您可能会在处理文本的非常短的Ruby程序中看到它们。
In short, you'll rarely see global variables. They're often bad form (and "un-Ruby") and only really useful in very small scripts, where the full implication of their use can be fully appreciated. There are a few special global variables that can be used, but for the most part, they aren't used. You don't really need to know all that much about global variables to understand most Ruby programs, but you should at least know that they're there.
简而言之,您很少会看到全局变量。 它们通常是错误的形式(和“ un-Ruby”),并且仅在非常小的脚本中才真正有用,在这些脚本中可以充分理解其用法的全部含义。 可以使用一些特殊的全局变量,但是在大多数情况下,它们并未使用。 您不需要真正了解全局变量就可以了解大多数Ruby程序,但是至少应该知道它们在那里。
ruby全局变量