本文简要介绍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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。