How to Get Multiple-Line User Input in Ruby

Ruby Multi-Line Input for Only One Input

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

How to get multiple-line user input in Ruby?

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.

Ruby 'gets' that works over multiple lines

You can do this in following way,

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

make sure to type END keyword when you think the input is finished,

As well this will only work with actual interpreter not irb.

How to get multiple input from one line in ruby

Easiest way would be to read one string, then split it into numbers. Something like this:

print "Enter three points for the triangle: "    
x1, y1, x2, y2, x3, y3 = gets.split.map(&:to_f)

How can I take multiple inputs from the same line?

gets reads in the whole line. If you want to process multiple elements from it, you need to split on that line, or perform regex matches on it, etc. In your case:

p, q = gets.split.map(&:to_i)

BTW, in your code, the chomp calls are superfluous, since to_i will work correctly whether the string ends with a newline or not.

Ruby: Can I write multi-line string with no concatenation?

There are pieces to this answer that helped me get what I needed (easy multi-line concatenation WITHOUT extra whitespace), but since none of the actual answers had it, I'm compiling them here:

str = 'this is a multi-line string'\
' using implicit concatenation'\
' to prevent spare \n\'s'

=> "this is a multi-line string using implicit concatenation to eliminate spare
\\n's"

As a bonus, here's a version using funny HEREDOC syntax (via this link):

p <<END_SQL.gsub(/\s+/, " ").strip
SELECT * FROM users
ORDER BY users.id DESC
END_SQL
# >> "SELECT * FROM users ORDER BY users.id DESC"

The latter would mostly be for situations that required more flexibility in the processing. I personally don't like it, it puts the processing in a weird place w.r.t. the string (i.e., in front of it, but using instance methods that usually come afterward), but it's there. Note that if you are indenting the last END_SQL identifier (which is common, since this is probably inside a function or module), you will need to use the hyphenated syntax (that is, p <<-END_SQL instead of p <<END_SQL). Otherwise, the indenting whitespace causes the identifier to be interpreted as a continuation of the string.

This doesn't save much typing, but it looks nicer than using + signs, to me.

Also (I say in an edit, several years later), if you're using Ruby 2.3+, the operator <<~ is also available, which removes extra indentation from the final string. You should be able to remove the .gsub invocation, in that case (although it might depend on both the starting indentation and your final needs).

EDIT: Adding one more:

p %{
SELECT * FROM users
ORDER BY users.id DESC
}.gsub(/\s+/, " ").strip
# >> "SELECT * FROM users ORDER BY users.id DESC"

How can I have multiple lines in the ruby interactive shell?

This is an example:

2.1.2 :053 > a = 1
=> 1
2.1.2 :054 > b = 2
=> 2
2.1.2 :055 > a + b
=> 3
2.1.2 :056 > if a > b #The code ‘if ..." starts the definition of the conditional statement.
2.1.2 :057?> puts "false"
2.1.2 :058?> else
2.1.2 :059 > puts "true"
2.1.2 :060?> end #The "end" tells Ruby we’re done the conditional statement.
"true" # output
=> nil # returned value

IRB can tell us the result of the last expression it evaluated.

You can get more useful information from here(https://www.ruby-lang.org/en/documentation/quickstart/).

create two variables from user input in ruby

No, you're not doing it correctly. Ditch the methods. And the instance variables. Use local variables.

print "Enter your first and last name: "

first = gets.chomp
last = gets.chomp

print "Hello, " + first + last + "!"


Related Topics



Leave a reply



Submit