Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 尺寸是什么意思_Ruby_Windows - Fatal编程技术网

Ruby 尺寸是什么意思

Ruby 尺寸是什么意思,ruby,windows,Ruby,Windows,在下面的代码中,target.size是什么意思?文件名旁边的w是什么意思?当我删除w或.size程序时,这意味着什么 filename = ARGV.first script = $0 puts "We're going to erase #{filename}." puts "If you don't wnat that, hit CTRL-C (^C)." puts "If you do want that, hit RETURN." print

在下面的代码中,
target.size
是什么意思?文件名旁边的
w
是什么意思?当我删除
w
.size
程序时,这意味着什么

filename = ARGV.first
    script = $0

    puts "We're going to erase #{filename}."
    puts "If you don't wnat 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()
为您提供一个对象,该对象被分配给局部变量
target
target。size
表示实际大小,返回文件大小(以字节为单位)。如果不提供模式为
'w'
,则文件将以默认模式
'r'
打开


阅读:

“w”

仅写,将现有文件截断为零长度或创建新文件进行写入


解释行
target.truncate(target.size)


在这里,您实际调用了,这个方法所做的是将文件截断为最多整数字节。这意味着您正在完全删除文件内容,并将文件大小设置为零。

对不起,我不明白,我是ruby的专业人员,对不起,您能将其设置为simplerw=writable吗?但是大小意味着什么?为什么在我删除时代码没有崩溃。sizeIt不会。因为您要做的是删除旧内容,然后将新内容添加到该文件中。。