Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Kernel | gets

概要

Kernel#gets(rs = $/) -> String | nil

詳細

ARGFから一行読み込んで、それを返します。
rsは区切り文字。省略すると行区切りになる。
getsした内容は$_にも設定されている。

ファイルの終わりに到達するとnilを返却する

サンプルコード

検証用テキスト(sample.txt)

sample1_1, sample2_1
sample1_2, sample2_2
sample1_3, sample2_3

sample.ruby

require 'test_toolbox'
ret = gets
print ret
print $_

dp_line __LINE__

ret = gets(',')
print ret
puts
print $_
puts

__END__
下記はTbpgrUtils gemの機能
dp_line

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils
出力
$ ruby sample.rb sample.txt
sample1_1,sample2_1
sample1_1,sample2_1
--------------------|filename=|line=6|--------------------
sample1_2,
sample1_2,