O'Reilly logo

Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition, Video Enhanced 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 35. Branches and Functions

Image

You have learned if-statements, functions, and arrays. Now it’s time to bend your mind. Type this in, and see if you can figure out what it’s doing.

ex35.rb

 1    def gold_room  2      puts "This room is full of gold. How much do you take?"  3  4      print "> "  5      choice = $stdin.gets.chomp  6  7      # this line has a bug, so fix it  8      if choice.include? ("0") || choice.include? ("1")  9        how_much = choice.to_i 10      else 11        dead("Man, learn to type a number.") 12      end 13 14      if how_much < 50 15        puts "Nice, you're not greedy, you win!" ...

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