Ruby:STDIN.gets() 和 gets.chomp() 有什么区别?

编程入门 行业动态 更新时间:2024-10-24 22:28:33
本文介绍了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() 有什么区别?

本文发布于:2023-11-02 08:08:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551844.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   Ruby   STDIN   chomp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!