我有一个正在开发的小程序,我希望用户能够输入可能的多行响应。
我找到了例子
$/ = "END"
user_input = STDIN.gets
puts user_input
但这使得所有输入都需要 END 关键字,而我只需要一个输入。
如何只为一个输入生成多行输入?
最佳答案
IO#gets
有一个可选参数,允许您指定分隔符。这是一个例子:
puts "Enter Response"
response = gets.chomp
puts "Enter a multi line response ending with a tab"
response = gets("\t\n").chomp
输出:
Enter Response
hello
Enter a multi line response ending with a tab
ok
how
is
this
关于仅用于一个输入的 Ruby 多行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28442867/