Ruby 程式設計/參考/物件/IO
外觀
IO 類本質上是一個抽象類,它提供用於在流上使用的方法(例如,開啟檔案或開啟套接字)。
基本上,你可以在上面呼叫許多不同的東西。
請注意,在 1.9 中,每次呼叫都會返回一個帶有編碼集的字串,該編碼集基於建立連線的方式,例如
a = File.new('some filename', 'rb:ASCII-8BIT') # strings from this will be read in as ASCII-8BIT
b = File.new('some filename', 'r') # strings from this will be read in as whatever the Encoding.default_external is
# you can change what the encoding will be
a.set_encoding "UTF-8" # from now on it will read in UTF-8
gets 讀取一行,或者一直讀取到檔案/流的末尾(如果提供了換行符,則包含尾隨的換行符)。一個阻塞呼叫。
recv(1024) 讀取最多 1024 位元組,並返回你的字串。一個阻塞呼叫。如果套接字從另一端正常關閉,則讀取 ""。它還有非阻塞的夥伴。
read 讀取到檔案末尾或套接字關閉為止。返回任意數量的位元組。阻塞。有關如何避免阻塞的資訊,請參見Socket.