Exercise 12. Prompting People for Numbers

You may have noticed in Exercise 11 that you were getting only strings from the user. How do you get numbers? Type this code in to find out how.

ex12.rb

 1    print "Give me a number: "  2    number = gets.chomp.to_i  3  4    bigger = number * 100  5    puts "A bigger number is #{bigger}."  6  7    print "Give me another number: "  8    another = gets.chomp  9    number = another.to_i 10 11    smaller = number / 100 12    puts "A smaller number is #{smaller}."

What You Should See

Exercise 12 Session

$ ruby ex12.rb Give me a number: 10 A bigger number is 1000. Give me another number: 200 A smaller number is 2.

See how I can add a .to_i to ...

Get Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.