如何在Ruby中获得多行用户输入? [英] How to get multiple-line user input in Ruby?

查看:96
本文介绍了如何在Ruby中获得多行用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常简单的问题,但是我的google-fu让我失望了.我想在Ruby中获得多行用户输入,因为get仅能为您提供一行输入.我希望能够将多行输入存储到字符串中,以便可以将字符串输出到文件末尾.有可用的命令来执行此操作吗?预先感谢!

I know this is a really simple question but my google-fu is failing me. I want to get multiple line user input in Ruby, as gets only gives you one line of input. I want to be able to store this multiple line input into a string so i can output the string to the end of a file. Is there a command available to do this? Thanks in advance!

推荐答案

尝试一下:

$/ = "END"
user_input = STDIN.gets
puts user_input

然后,用户可以继续输入多行输入,并通过输入END结束输入.请记住,IRB不能很好地处理此代码段,因此请确保使用实际的ruby解释器.

The user can then keep entering a multiple line input and ends his input by typing in END. Keep in mind that IRB doesn't handle this code snippet well, so make sure you use the actual ruby interpreter.

user_input还存储用户输入的最终END,因此您需要将其删除.您还可以在END处使用任何其他字符串来表示用户已完成输入.

Also the user_input stores the final END typed in by the user so you'll need to strip it out. You can also use any other string in the place of END for the user to indicate that they've finished inputting.

这篇关于如何在Ruby中获得多行用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆