简体   繁体   English

.size是什么意思

[英]What Does .size mean

What does target.size mean in the below code and what does w mean that is next to the filename? 以下代码中的target.size是什么意思,文件名旁边的w是什么意思? And when I remove w or .size the program what does that mean? 当我删除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()

File.open(filename,'w') gives you a File object, which is assigned to the local variable target . File.open(filename,'w')给您一个File对象,该对象分配给局部变量target target.size means actually File#size , which Returns the size of file in bytes. target.size实际上是File#size ,它返回文件的大小(以字节为单位)。 If you don't supply the mode as 'w' , then the file will be opened with mode 'r' , which is a default mode. 如果未将模式提供为'w' ,则文件将以'r'模式打开,这是默认模式。


Read IO Open Mode : 读取IO打开模式

"w" : "w"

Write-only, truncates existing file to zero length or creates a new file for writing. 只写,将现​​有文件截断为零长度或创建一个新文件进行写入。


Explanation of the line target.truncate(target.size) . 关于target.truncate(target.size)行的说明。

Here you actually called File#truncate , what this method does is Truncates file to at most integer bytes . 在这里,您实际上称为File#truncate ,此方法的作用是将文件截断为最大整数字节 That means you are deleting the file contents fully and making the file size as zero . 这意味着您将完全删除文件内容并使文件大小为零

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM