gpt4 book ai didi

ruby 'script = $0'

转载 作者:数据小太阳 更新时间:2023-10-29 07:02:57 24 4
gpt4 key购买 nike

我在查看 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()

最佳答案

$0 是 Ruby 的全局变量之一。来自 here :

$0 -- Contains the name of the script being executed. May be assignable.

关于 ruby 'script = $0',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10364802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com