るりまサーチ

最速Rubyリファレンスマニュアル検索!
11件ヒット [1-11件を表示] (0.070秒)
トップページ > クエリ:IO.new[x] > クエリ:>>[x] > クエリ:reopen[x] > 種類:変数[x]

別のキーワード

  1. argf.class lines
  2. argf.class each
  3. argf.class each_line
  4. argf.class gets
  5. argf.class readline

ライブラリ

モジュール

検索結果

Kernel$$stdin -> object (19.0)

標準入力です。

...fileno, to_io, eof, each_line, each_byte,
binmode, closed?

//emlist[例][ruby]{
$stdin = Object.new
def $stdin.gets
"foo"
end
p gets() # => "foo"
//}

自プロセスだけでなく、子プロセスの標準入力もリダイレクトしたいときは
以下のように IO#reopen を使い...
...//emlist[例][ruby]{
$stdin.reopen("/tmp/foo")
//}

また、リダイレクトしたあと
出力先をまた元に戻したい場合は以下のようにします。

//emlist[例][ruby]{
stdin_old = $stdin.dup # 元の $stdin を保存する
$stdout.reopen("/tmp/foo") # $stdin を /tmp/...
...foo にリダイレクトする
gets # /tmp/foo から入力
$stdin.reopen stdin_old # 元に戻す
//}

$stdin はグローバルスコープです。...