将单行用户输入的字符串拆分为整数?
我刚刚开始学习一些 ruby,我想做这样的事情:
print "Insert two numbers: "
a, b = gets.split(" ")
但我想让 a 和 b 同时成为整数(在同一行)..如果我添加 .to_i
到第二行(在 split(" ")
之前或之后),它不起作用......那么,我应该如何处理这个问题?映射、分割、切片?好的,我知道我可以使用 scanf,但是除了 scanf,我该怎么做?
很抱歉提出了这样一个新手问题,但我只是无法找到足够好的答案,只能通过谷歌搜索......
I just started learning some ruby, and I want to do something like this:
print "Insert two numbers: "
a, b = gets.split(" ")
but I want to make a and b to be integers at the same time (in the same line).. If I add .to_i
to the second line (before or after split(" ")
), it doesn't work... so, how should I approach this? mapping, splitting, slicing? ok, I know I could use scanf, but other than scanf, how would I do this?
sorry for such a noobish question, but I just couldn't find a good enough answer only googling...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者更通用:
Or more universal:
这应该有效
This should work