How to get multiple-line user input in Ruby?

2020-03-27 01:58发布

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!

标签: ruby input
1条回答
来,给爷笑一个
2楼-- · 2020-03-27 02:35

Try this:

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

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.

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.

查看更多
登录 后发表回答