在Ruby中,如果为变量分配函数,为什么会自动运行?

时间:2016-10-31 19:17:43

标签: ruby methods irb

使用以下代码:

variable = puts "hello world".upcase

为什么Ruby会自动将Hello world置于upcase中,而不首先调用变量?我知道你正在将函数设置为变量,如果调用该变量,它将返回返回值(在本例中为nil),但为什么Ruby几乎未经许可就运行方法puts "hello world".upcase(没有调用它,只是分配给一个变量)?

1 个答案:

答案 0 :(得分:5)

您没有为变量分配函数。

这与

相同
variable = (puts("hello world".upcase))

需要执行puts将返回值赋给变量变量(lol)。

这是一种为变量分配方法的方法。

puts_method = method(:puts)
相关问题