當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby IO.ungetbyte用法及代碼示例

本文簡要介紹ruby語言中 IO.ungetbyte 的用法。

用法

ungetbyte(string) → nil
ungetbyte(integer) → nil

將字節(作為參數傳遞)推回 ios ,以便後續緩衝讀取將返回它。僅保證支持單個字節,並且僅當自從上次從 ios 讀取至少一個字節後尚未在 ios 上調用 ungetbyte 或 ungetc 時。但是,如果內部緩衝區中有空間允許它,它可以支持額外的字節。

f = File.new("testfile")   #=> #<File:testfile>
b = f.getbyte              #=> 0x38
f.ungetbyte(b)             #=> nil
f.getbyte                  #=> 0x38

如果給定一個整數,則僅使用整數的低 8 位作為要推送的字節。

f = File.new("testfile")   #=> #<File:testfile>
f.ungetbyte(0x102)         #=> nil
f.getbyte                  #=> 0x2

調用此方法會添加到現有緩衝區,即使之前已經調用過該方法:

f = File.new("testfile")   #=> #<File:testfile>
f.ungetbyte("ab")          #=> nil
f.ungetbyte("cd")          #=> nil
f.read(5)                  #=> "cdab8"

對無緩衝讀取(例如 IO#sysread )無效。

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 IO.ungetbyte。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。