Ruby'脚本= $ 0'

use*_*142 6 ruby

我正在看一个Ruby脚本,我遇到了script = $0.我做了一些谷歌搜索,但我没有找到一个明确的答案,这是做什么的.我相信它可以保护你免于读取比内存更大的文件,这是正确的吗?

谢谢,我有完整的脚本,所以你可以在上下文中看到它:

# Takes the name of a file as an argument and assigns to filename 
filename = ARGV.first 
script = $0

puts "We're going to erase #{filename}."
puts "If you don't want that, hit CTRL-C (^C)."
puts "If you do want that, hit RETURN."

print "? "
STDIN.gets

puts "Opening the file..."
target = File.open(filename, 'w')

puts "Truncating the file. Goodbye!"
target.truncate(target.size)

puts "Now I'm going to ask you for three lines."

print "line 1: "; line1 = STDIN.gets.chomp()
print "line 2: "; line2 = STDIN.gets.chomp()
print "line 3: "; line3 = STDIN.gets.chomp()

puts "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

puts "And finally, we close it."
target.close()
Run Code Online (Sandbox Code Playgroud)

KL-*_*L-7 13

$0是Ruby的全局变量之一.从这里:

$ 0 - 包含正在执行的脚本的名称.可以分配.