ruby全局变量_Ruby中的全局变量

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值