Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

เชี่ยวชาญ Temporary Files ใน Ruby: คู่มือปฏิบัติจริง

สร้าง Temporary File = ได้ Empty File ชื่อ Random ใน OS Temp Folder — File นี้ ลบอัตโนมัติ

ทำใน Ruby ยังไง? แบบนี้:

require 'tempfile'
Tempfile.create { |f| f << "abc\n" }

f = File, << เขียน — Built-in ไม่ต้อง Install Gems

Simple แตยังมีคำถาม — เช่น:

  • File ถูกลบเมื่อไหร่พอดี?
  • ทำไมอ่าน Temp File กลับไม่ได้?
  • Tempfiles รับประกัน Unique ไหม?

ตอบคำถามเหล่านี้และอีกเพื่อเข้าใจลึก!

New vs Create ต่างกันยังไง?

Tempfile มี 2 Methods สร้างไฟล์ — new และ create

ต่างกันตรงไหน? Doc: new สร้าง Tempfile Object (ตามปกติ) แต create ให้ File Object

คิดว่าไม่สำคัญมาก เพราะ Tempfile Delegate ไป File — ความต่างจริง: create รับ Block, new ไม่รับ

ลอง: Tempfile.new {} — Warning: Tempfile.new doesn't call the given block.

用 Block กับ create 确保 Temp File ลบหลัง Block จบ — นำไปสู่คำถาม...

TempFile ถูกลบเมื่อไหร่?

ควบคุมได้: ลบเองเหมือน Regular File (delete Method) หรือ 用 create กับ Block

Auto Delete เมื่อ:

  • Program จบ
  • File ถูก "Garbage Collected" (อัลกอริทึมลบจาก Memory เผื่อพื้นที่)

หล者 发生 ก็ต่อเมื่อไม่ Hold References ไว้ — เช่น Variable

หมายถึง: t = Tempfile.new — ตอน t Out of Scope Temp File ถูกลบได้

Linux มี Tool น่าสนใจ ดู Files Created/Deleted Real-time: inotify-tools

Command:

inotifywait /tmp -m --format "%w %e %f %T" --timefmt "%H:%m:%S"

รัน Ruby Code สร้างไฟล์ — ตัวอย่าง:

ruby -rtempfile -e "def c; t = Tempfile.new('testing'); end; c; sleep 10"

เห็น:

/tmp/ CREATE testing20190506-11391-1wqcng0 14:51:48
/tmp/ OPEN testing20190506-11391-1wqcng0 14:51:48
/tmp/ CLOSE_WRITE,CLOSE testing20190506-11391-1wqcng0 14:51:58
/tmp/ DELETE testing20190506-11391-1wqcng0 14:51:58

ทำไมอ่าน Temp File กลับไม่ได้?

ลองอ่านกลับจาก Temp File ได้ Empty String

ตัวอย่าง: Tempfile.create { |f| f << "abc\n"; f.read } # ""

ทำไม? Files เป็น IO Objects — มี Position Pointer เขียน File Pointer ก้อนหน้า

อยากอ่านต้อง Rewind Pointer — แบบนี้:

Tempfile.create { |f| f << "abc\n"; f.rewind; f.read } # "abc\n"

อาจต้อง Flush Contents ด้วย — temp.flush

Tempfiles รับประกัน Unique จริงไหม?

Tempfile สร้าง Unique Filename Permission Mode 600 = User สร้างเท่านั้นอ่านได้

แตมี Duplicate Name ไหม? Doc บอก:

"Tempfile's filename picking method is both thread-safe and inter-process-safe: guarantees no other threads or processes pick the same filename."

new Description: อาจ Raise Error ถ้าหา Unique Filename ไม่ได้ — 設 Prefix ได้: Tempfile.new("banana-").path # "/tmp/banana-20190426-25403-1cm7sjt" ลดโอกาส Collision

เปิด Tempfile ใน Binary Mode

ทำงานกับ Images, Music, Non-Plain-Text — ตั้ง File Mode เป็น Binary

Binary Mode หยุด Line-ending Conversion — Data "Raw" กันแตก Binary File

Binary Mode ไม่ Enable By Default: temp.binmode? # false

Enable: temp.binmode # true — กับ create ก็ได้: Tempfile.create { |f| f.binmode; f << "bacon\n" }

Summary (สรุป)

คุณเรียนรู้ Temporary Files ใน Ruby ด้วย Class Tempfile!

ตอนนี้ถึงคุณ Practice — ขอบคุณที่อ่าน 🙂