在 Ruby 中使用 gets 命令按字母顺序对数组进行排序
我是一个 Ruby 菜鸟,我只是尝试使用 gets 命令对单词数组(“dog”、“cat”、“ape”)进行排序,应该通过 gets 单独输入并成为(“ape”、“cat”、 “狗”)
我已经尝试过:
list = Object.new
list = []
word = STDIN.gets
list.push(word)
$/ = "END"
puts list
任何帮助都会很棒,因为这可以帮助我女儿更快地整理作业并学习打字。
I am a Ruby noob and am simply trying to use the gets command to sort a array of words ("dog", "cat", "ape") should be entered individually by gets and become ("ape", "cat", "dog")
I have tried:
list = Object.new
list = []
word = STDIN.gets
list.push(word)
$/ = "END"
puts list
Any help would be great as this is to help my daughter sort her homework faster and learn to type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您愿意,您也可以一次输入所有单词:
如果您想单独阅读它们:
这只是从 IRB 复制/粘贴,但很容易制作成您想要的程序。
You can also enter all the words at once if you want to:
If you want to read them individually:
That's just copy/paste from IRB, but easy enough to make into the program you want.
这将接受输入,直到您给出“END”(您可以根据需要更改它)。
我正在调用 Array#sort
This will take input until you give it "END" (you can change this as you wish).
I am calling Array#sort