The built-in gets
function simply reads a line and returns it as a string. The chomp
method from class String trims the line terminator. The prompts are generated with ordinary prints lacking a final newline.
# Get the parts of speech print "Please enter a past-tense verb: " verb = gets.chomp print "Please enter a noun: " noun = gets.chomp print "Please enter a proper noun: " prop_noun = gets.chomp print "Please enter a an adverb: " adv = gets.chomp # Make the sentence. print "#{prop_noun} got a #{noun} and\n#{verb} #{adv} around the block.\n"
Similarly, the to_f
method of class String converts the input string to a floating-point number.
print "Triangle height: " h = gets.to_f; print "Triangle width: " w = gets.to_f; area = 0.5*h*w print "Triangle height ", h, " width ", w, " has area ", area, "\n"