Ruby: What's the difference between STDIN.gets() and gets.chomp()?

前端 未结 3 1829
不知归路
不知归路 2020-12-15 12:00

What\'s the difference between STDIN.gets() and gets.chomp() in Ruby? Aren\'t they both retrieving raw input from the user?

side question:

3条回答
  •  情书的邮戳
    2020-12-15 12:27

    gets is actually Kernel#gets. It reads from files passed as arguments or, if no arguments are present, reads from standard input. If you want to read only from standard input, then you should be more explicit about it.

    STDIN.gets
    $stdin.gets
    

    As for the conversion, I normally use String#to_i. It handles newlines just fine.

提交回复
热议问题