[英]What Does .size mean
以下代碼中的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()
File.open(filename,'w')
給您一個File
對象,該對象分配給局部變量target
。 target.size
實際上是File#size
,它返回文件的大小(以字節為單位)。 如果未將模式提供為'w'
,則文件將以'r'
模式打開,這是默認模式。
讀取IO打開模式 :
"w"
:
只寫,將現有文件截斷為零長度或創建一個新文件進行寫入。
關於
target.truncate(target.size)
行的說明。
在這里,您實際上稱為File#truncate
,此方法的作用是將文件截斷為最大整數字節 。 這意味着您將完全刪除文件內容並使文件大小為零 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.