本文簡要介紹ruby語言中 IO.new 的用法。
用法
new(fd [, mode] [, opt]) → io
為給定的整數文件說明符 fd 和 mode 字符串返回一個新的 IO 對象(流)。 opt 可用於以更易讀的方式指定mode 的部分。另請參見 IO.sysopen 和 IO.for_fd 。
IO.new 由各種 File 和 IO 打開方法調用,例如 IO::open 、 Kernel#open 和 File::open 。
開放模式
當 mode 是整數時,它必須是在 File::Constants ( File::RDONLY , File::WRONLY|File::CREAT ) 中定義的模式的組合。有關更多信息,請參見 open(2) 手冊頁。
當mode 是字符串時,它必須是以下形式之一:
fmode fmode ":" ext_enc fmode ":" ext_enc ":" int_enc fmode ":" "BOM|UTF-*"
fmode 是 IO 開放模式字符串,ext_enc 是 IO 的外部編碼,int_enc 是內部編碼。
IO 打開模式
Ruby 允許以下打開模式:
"r" Read-only, starts at beginning of file (default mode).
"r+" Read-write, starts at beginning of file.
"w" Write-only, truncates existing file
to zero length or creates a new file for writing.
"w+" Read-write, truncates existing file to zero length
or creates a new file for reading and writing.
"a" Write-only, each write call appends data at end of file.
Creates a new file for writing if file does not exist.
"a+" Read-write, each write call appends data at end of file.
Creates a new file for reading and writing if file does
not exist.
以下模式必須單獨使用,並與上述一種或多種模式一起使用。
"b" Binary file mode
Suppresses EOL <-> CRLF conversion on Windows. And
sets external encoding to ASCII-8BIT unless explicitly
specified.
"t" Text file mode
獨占訪問模式(“x”)可以與“w”一起使用,以確保文件被創建。 Errno::EEXIST 在它已經存在時引發。它可能不支持所有類型的流(例如管道)。
原 IO 的打開方式為隻讀時,不能改為可寫方式。同樣,打開模式不能從隻寫變為可讀。
當嘗試進行此類更改時,會根據平台在不同位置引發錯誤。
IO Encoding
當指定ext_enc 時,讀取的字符串會在讀取時被編碼標記,而輸出的字符串會在寫入時轉換為指定的編碼。
當指定ext_enc和int_enc時,輸入時讀取字符串將從ext_enc轉換為int_enc,輸出時將寫入字符串從int_enc轉換為ext_enc。有關輸入和輸出轉碼的更多詳細信息,請參閱 Encoding 。
如果使用 “BOM|UTF-8”、“BOM|UTF-16LE” 或 “BOM|UTF16-BE”,Ruby 會檢查輸入文檔中的 Unicode BOM 以幫助確定編碼。對於 UTF-16 編碼,文件打開模式必須是二進製的。當存在時,將剝離 BOM 並使用 BOM 中的外部編碼。當 BOM 丟失時,給定的 Unicode 編碼用作 ext_enc 。 (BOM-set 編碼選項不區分大小寫,因此“bom|utf-8” 也是有效的。)
選項
可以使用opt 代替mode 以提高可讀性。支持以下鍵:
- :模式
-
與
mode參數相同 - :標誌
-
將文件打開標誌指定為整數。如果給定
mode參數,則該參數將為bitwise-ORed。 - :external_encoding
-
IO的外部編碼。 - :internal_encoding
-
IO的內部編碼。 “-” 是默認內部編碼的同義詞。如果值為
nil,則不會發生轉換。 - :編碼
-
將外部和內部編碼指定為 “extern:intern”。
- :文本模式
-
如果該值為真值,則與參數
mode中的 “t” 相同。 - :binmode
-
如果該值為真值,則與參數
mode中的 “b” 相同。 - :自動關閉
-
如果值為
false,則fd將在此IO實例完成後保持打開狀態。
此外,opt 可以在 String#encode 中具有相同的鍵,用於控製外部編碼和內部編碼之間的轉換。
示例 1
fd = IO.sysopen("/dev/tty", "w")
a = IO.new(fd,"w")
$stderr.puts "Hello"
a.puts "World"
產生:
Hello
World
示例 2
require 'fcntl'
fd = STDERR.fcntl(Fcntl::F_DUPFD)
io = IO.new(fd, mode: 'w:UTF-16LE', cr_newline: true)
io.puts "Hello, World!"
fd = STDERR.fcntl(Fcntl::F_DUPFD)
io = IO.new(fd, mode: 'w', cr_newline: true,
external_encoding: Encoding::UTF_16LE)
io.puts "Hello, World!"
以上兩個都打印“Hello, World!”在 UTF-16LE 到標準錯誤輸出,將 puts 生成的 EOL 轉換為 CR。
相關用法
- Ruby IO.noecho用法及代碼示例
- Ruby IO.eof用法及代碼示例
- Ruby IO.read用法及代碼示例
- Ruby IO.fileno用法及代碼示例
- Ruby IO.pread用法及代碼示例
- Ruby IO.raw用法及代碼示例
- Ruby IO.readlines用法及代碼示例
- Ruby IO.to_i用法及代碼示例
- Ruby IO.self << object用法及代碼示例
- Ruby IO.tty?用法及代碼示例
- Ruby IO.close_write用法及代碼示例
- Ruby IO.write_nonblock用法及代碼示例
- Ruby IO.set_encoding_by_bom用法及代碼示例
- Ruby IO.syswrite用法及代碼示例
- Ruby IO.close_read用法及代碼示例
- Ruby IO.stat用法及代碼示例
- Ruby IO.pwrite用法及代碼示例
- Ruby IO.ungetc用法及代碼示例
- Ruby IO.sysopen用法及代碼示例
- Ruby IO.try_convert用法及代碼示例
- Ruby IO.write用法及代碼示例
- Ruby IO.pathconf用法及代碼示例
- Ruby IO.sysseek用法及代碼示例
- Ruby IO.closed?用法及代碼示例
- Ruby IO.sync =用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 IO.new。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
