Mô hình var toàn cầu global var là gì

Many teams struggle to manage product quality within shorter and shorter time-to-market windows, but best-in-class test organizations are leading the way.

Read our featured article

A global variable has a name beginning with $. It can be referred to from anywhere in a program. Before initialization, a global variable has the special value`nil`.

ruby> $foo nil ruby> $foo = 5 5 ruby> $foo 5

Global variables should be used sparingly. They are dangerous because they can be written to from anywhere. Overuse of globals can make isolating bugs difficult; it also tends to indicate that the design of a program has not been carefully thought out. Whenever you do find it necessary to use a global variable, be sure to give it a descriptive name that is unlikely to be inadvertently used for something else later (calling it something like $foo as above is probably a bad idea).

One nice feature of a global variable is that it can be traced; you can specify a procedure which is invoked whenever the value of the variable is changed.

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

When a global variable has been rigged to work as a trigger to invoke a procedure whenever changed, we sometimes call it an active variable. For instance, it is useful for keeping a GUI display up to date.

There is a collection of special variables whose names consist of a dollar sign ($) followed by a single character. For example,$$ contains the process id of the ruby interpreter, and is read-only. Here are the major system variables and their meanings (see the ruby reference manual for details):

$! latest error message $@ location of error $_ string last read by

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

0

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

1 line number last read by interpreter

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

2 string last matched by regexp

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

3 the last regexp match, as an array of subexpressions `$`n the nth subexpression in the last match (same as

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

5n

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

7 case-insensitivity flag

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

8 input record separator

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

9 output record separator $`0 the name of the ruby script file $1 the command line arguments $$ interpreter's process ID $`3 exit status of last executed child process

In the above, $_ and

ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"} nil ruby> $x = 5 $x is now 5 5

3 have local scope. Their names suggest they should be global, but they are much more useful this way, and there are historical reasons for using these names.

Một biến toàn cầu là một cấu trúc ngôn ngữ lập trình, một loại biến được khai báo bên ngoài bất kỳ chức năng và có thể truy cập đến tất cả các chức năng trong suốt chương trình. Một nhóm các biến toàn cục được gọi là một nhà nước toàn cầu hoặc môi trường toàn cầu vì khi kết hợp, họ xác định các khía cạnh khác nhau của một chương trình hoặc môi trường khi chương trình chạy. Một biến toàn cầu thường được khai báo trên đầu trang của tất cả các chức năng và được giữ ở mức tối thiểu, như tất cả các chức năng có thể điều khiển chúng trong thời gian chạy của chương trình, đó được coi là nguy hiểm bởi hầu hết các lập trình viên vì họ vô tình có thể thay đổi, dẫn đến lỗi.

Xem thêm: Thuật ngữ công nghệ A-Z

Giải thích ý nghĩa

Các biến toàn cục, như tên của nó, là các biến có thể truy cập trên toàn cầu, hoặc ở khắp mọi nơi trong suốt chương trình. Từng tuyên bố, họ vẫn trong bộ nhớ trong suốt thời gian chạy của chương trình. Điều này có nghĩa rằng họ có thể được thay đổi bằng bất kỳ chức năng bất cứ lúc nào và có thể ảnh hưởng đến chương trình như một toàn thể. Trong những năm đầu của máy tính mà bộ nhớ được rất hạn chế, họ đã coi thực hành xấu vì họ đã giải phóng không gian bộ nhớ có giá trị và nó rất dễ dàng cho các lập trình viên để theo dõi mất các giá trị của họ, đặc biệt là trong các chương trình dài, dẫn đến lỗi mà có thể rất khó để xác định vị trí. mã nguồn được hiểu tốt nhất khi phạm vi của các yếu tố cá nhân của nó bị hạn chế, vì vậy vì không địa phương của họ, rất khó để theo dõi nơi họ đã được thay đổi hoặc lý do tại sao họ đã thay đổi.

What is the Global Variable? - Definition

A global variable is a programming language construct, a variable type that is declared outside any function and is accessible to all functions throughout the program. A group of global variables is called a global state or global environment because when combined, they define various aspects of a program or the environment when the program runs. A global variable is usually declared on top of all functions and is kept to a minimum, as all functions can manipulate them during the program’s run time, which is considered dangerous by most programmers because they may accidentally be changed, resulting in bugs.

Understanding the Global Variable

Global variables, as the name implies, are variables that are accessible globally, or everywhere throughout the program. Once declared, they remain in memory throughout the runtime of the program. This means that they can be changed by any function at any point and may affect the program as a whole. During the early years of computers where memory was very limited, they became considered bad practice because they took up valuable memory space and it was very easy for the programmer to lose track of their values, especially in long programs, leading to bugs that can be very hard to locate. Source code is best understood when the scope of its individual elements are limited, so because of their non-locality, it is hard to keep track of where they have been changed or why they were changed.