Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Multi-Line Input for Only One Input

Tags:

ruby

input

I have a small program that I'm working on that at one point I would like the user to be able to input a potentially multiline response.

I've found the example with

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

But this makes all inputs require the END keyword, which I would only need for the one input.

How can I produce a multi-line input for just the one input?

like image 966
Milksnake12 Avatar asked Dec 20 '22 06:12

Milksnake12


1 Answers

IO#gets has an optional parameter that allows you to specify a separator. Here's an example:

puts "Enter Response"
response = gets.chomp

puts "Enter a multi line response ending with a tab"
response = gets("\t\n").chomp

Output:

Enter Response
hello
Enter a multi line response ending with a tab
ok
how
is
this
like image 200
Anthony Avatar answered Dec 28 '22 08:12

Anthony