本文介绍了Ruby:STDIN.gets() 和 gets.chomp() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
STDIN.gets() 和 gets.chomp() 在 Ruby 中有什么区别?他们不是都从用户那里检索原始输入吗?
What's the difference between STDIN.gets() and gets.chomp() in Ruby? Aren't they both retrieving raw input from the user?
附带问题:如果我想将他们的输入转换为整数,我可以这样做
side question: If I want to convert their input into an integer, do I do
myNumb = Integer(STDIN.gets())和
myNumb = Integer(gets.chomp()) 推荐答案执行您在此处描述的操作的最简单方法是 Integer(gets),因为 Integer() 忽略尾随换行符,所以 chomp 是不必要的.也不需要明确指定 STDIN 作为接收者,因为这就是 Kernel#gets 就可以了.
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.
更多推荐
Ruby:STDIN.gets() 和 gets.chomp() 有什么区别?
发布评论