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.

When you’re ready, have a look at the solution.

Exercise 2: IRB Session

Launch irb from your terminal, and try the following expressions:

> my_string = "ruby is so cool"
> my_string.methods
> my_string.next
> my_string.reverse.upcase
> my_string.insert(11, "very, very ")
> "Rolling a die... we got #{ rand(1..6) }"
> "There are #{ 60 * 60 } seconds in an hour."
> 42.to_s
> "42".to_i

After you try them out, you can see an explanation of the results.