I saw two ways to use gets
, a simple form:
print 'Insert your name: '
name = gets()
puts "Your name is #{name}"
and a form that drew my attention:
print 'Insert your name: '
STDOUT.flush
name = gets.chomp
puts "Your name is #{name}"
The second sample looks like perl in using the flush
method of the default output stream. Perl makes explicit default output stream manipulating; the method flush
is a mystery to me. It can behave different from what I'm inferring, and it uses chomp
to remove the new line character.
What happens behind the scenes in the second form? What situation is it useful or necessary to use the second form?