O'Reilly logo

Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required