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.


 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 ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.