Exercise 35. Branches and Functions

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!" 16        exit(0) 17      else 18        dead("You greedy bastard!") 19      end 20    end 21 22 23     ...

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.