通过实例学习 ruby 过程中,看到 BEGIN 与 END 的关键字,也大致了解其含义与用法,但是在网上还是很难找到其相关资料。这是什么原因呢?下面是我在一个网站上看到的相关解释,让我不解: While the ruby program is loading, you can execute code inside a special block labeled "BEGIN" - pretty nifty. After the interpretor has loaded the code, but before execution, you can execute code in the "END" block.
puts "main program running" END { puts "program ending" } BEGIN { puts "I'm loading" }
输出: I'm loading main program running program ending
上面的解释:“After the interpretor has loaded the code, but before execution”说 END 中的 block 是在加载完成后并且执行前执行。这让人费解。有人能指点下么