本文簡要介紹ruby語言中 Kernel.gets
的用法。
用法
gets(sep=$/ [, getline_args]) → string or nil
gets(limit [, getline_args]) → string or nil
gets(sep, limit [, getline_args]) → string or nil
從 ARGV
(或 $*
)中的文件列表中返回(並分配給 $_
)下一行,或者如果命令行上沒有文件,則從標準輸入中返回。在文件末尾返回 nil
。可選參數指定記錄分隔符。分隔符包含在每條記錄的內容中。 nil
的分隔符讀取整個內容,零長度分隔符一次讀取輸入一個段落,其中段落被兩個連續的換行符分隔。如果第一個參數是一個整數,或者給出了可選的第二個參數,則返回的字符串不會長於給定的字節值。如果 ARGV
中存在多個文件名,則 gets(nil)
將一次讀取一個文件的內容。
ARGV << "testfile"
print while gets
產生:
This is line one
This is line two
This is line three
And so on...
使用$_
作為隱式參數的編程風格在Ruby 社區中逐漸失寵。
相關用法
- Ruby Kernel.gem用法及代碼示例
- Ruby Kernel.global_variables用法及代碼示例
- Ruby Kernel.local_variables用法及代碼示例
- Ruby Kernel.Integer用法及代碼示例
- Ruby Kernel.binding用法及代碼示例
- Ruby Kernel.frozen?用法及代碼示例
- Ruby Kernel.`cmd`用法及代碼示例
- Ruby Kernel.autoload用法及代碼示例
- Ruby Kernel.loop用法及代碼示例
- Ruby Kernel.Hash用法及代碼示例
- Ruby Kernel.caller用法及代碼示例
- Ruby Kernel.set_trace_func用法及代碼示例
- Ruby Kernel.exit!用法及代碼示例
- Ruby Kernel.trap用法及代碼示例
- Ruby Kernel.String用法及代碼示例
- Ruby Kernel.select用法及代碼示例
- Ruby Kernel.syscall用法及代碼示例
- Ruby Kernel.then用法及代碼示例
- Ruby Kernel.sprintf用法及代碼示例
- Ruby Kernel.Pathname用法及代碼示例
- Ruby Kernel.srand用法及代碼示例
- Ruby Kernel.yield_self用法及代碼示例
- Ruby Kernel.BigDecimal用法及代碼示例
- Ruby Kernel.raise用法及代碼示例
- Ruby Kernel.test用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Kernel.gets。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。