0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】条件分岐内で標準出力を使う方法

Last updated at Posted at 2020-08-09

アルゴリズム問題を解くときのようにgetsを使って標準入力を入れようとしたところ以下のようなエラーになった

$ ruby enc_main.rb new
Traceback (most recent call last):
        2: from enc_main.rb:12:in `<main>'
        1: from enc_main.rb:12:in `gets'
enc_main.rb:12:in `gets': No such file or directory @ rb_sysopen - new (Errno::ENOENT)

対処方法

以下のコードに$stdinを使ったgetsメソッドを使うと解消した。

if ARGV[0] == "new"
  puts "What's Name?"
  name = $stdin.gets

参考にしたドキュメント

$stdin (Ruby 2.7.0 リファレンスマニュアル)
Rubyでstdinを使う方法を現役エンジニアが解説【初心者向け】 | TechAcademyマガジン

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?