Chapter 1 - More With Less

Exercise 1: Reverse-o-Matic

The goal for your first exercise is to write a script that reverses text. Here’s what it should do:

  • Prompt the user to type a phrase and press Enter.
  • Use the gets method to read the string the user entered.
  • Call chomp on the user’s string to remove the newline character from the end.
  • Call reverse on the string, and display the result.

Solution

puts "Enter a phrase:"
input = gets
phrase = input.chomp
puts "Here is your phrase, backwards:"
puts phrase.reverse