How to Use "Gets" and "Gets.Chomp" in Ruby

How to use gets and gets.chomp in Ruby

gets lets the user input a line and returns it as a value to your program. This value includes the trailing line break. If you then call chomp on that value, this line break is cut off. So no, what you have there is incorrect, it should rather be:

  1. gets gets a line of text, including a line break at the end.
    • This is the user input
  2. gets returns that line of text as a string value.
  3. Calling chomp on that value removes the line break

The fact that you see the line of text on the screen is only because you entered it there in the first place. gets does not magically suppress output of things you entered.

How do I combine gets.chomp and ARGV in Ruby?

I answered a question about this yesterday, which you can read here, but to address your situation specifically:

After first, second, third = ARGV, call ARGV.clear to empty it out.

Alternatively you could do first, second, third = 3.times.map { ARGV.shift }

The reason is that gets reads from ARGV if there's anything in it. You need to empty ARGV out before calling gets.

Check if a number entered by the user with gets.chomp is a float in ruby

puts "What are the first number you want to divide"
number1 = gets.chomp.to_i
=> 100
puts "What is the second number?"
number2 = gets.chomp.to_i
=> 3

# Regular math
result_a = number1 / number2
puts "#{number1} / #{number2} = #{result_a}"
=> 100 / 3 = 33 # Integer class persists...

# Use a ruby library instead! Numeric#divmod
result_b = number1.divmod(number2)
puts "Result: #{result_b}"
=> [33, 1] # [quotient, modulus]

Gets.chomp() user input error in Ruby

I take it that your question is: "All my prompts are printed all at once after all inputs. What's up with that?". I have an answer for you then :)

print doesn't add a newline to the string. And STDOUT doesn't flush until it's got a full line. Simple fix: replace print with puts (which does add newline char)

puts "How old are you? "
age = gets.chomp()
puts "How tall are you?"
height = gets.chomp()
puts "How much do you weigh?"
weight = gets.chomp()
puts "So, you're #{age} old, #{height} tall and #{weight} heavy."

Ruby- gets issue

You can write your own Python equivalent input method:

def input(prompt)
print(prompt) # Output prompt
$stdout.flush # Flush stdout buffers to ensure prompt appears
gets.chomp # Get user input, remove final newline with chomp
end

Now we can try it:

name = input('What is your name? ')
puts "Welcome #{name}"

For more information on the methods used here. See these:

  • IO.flush
  • String.chomp

gets.chomp invoked first

Set $stdout.sync = true to force everything you write to stdout (after that point) to be immediately flushed.

By default ruby will buffer I/O if it thinks it's writing to something non-interactive, because that improves performance when you're e.g. writing to a file.

In this case, it sounds like it's guessing wrong... but it also sounds like your application is unlikely to benefit from ever buffering, so it's safe to just override the default to always be synchronous.



Related Topics



Leave a reply



Submit