MC logo

Basic I/O I

  Ruby Example Code

<<Line Breaking io2.rb Basic I/O II>>
# 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"
See:Ruby Manual [1][2]

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.
<<Line Breaking Basic I/O II>>