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

前端 未结 3 1828
不知归路
不知归路 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:47

    Easiest way to do what you describe here is Integer(gets), since Integer() ignores the trailing newline, so chomp is unnecessary. There's also no need explicitly specify STDIN as the receiver, as that's what Kernel#gets will do if there are no arguments to the script.

提交回复
热议问题