From: Stu Date: 2011-05-08T16:22:55+09:00 Subject: Re: Lets play a guessing game. (how to code this better?) I also wanted to add that you can embed methods directly into the argument area. following my previous example: x = add_num( 42, 100) add_num( x, add_num(16, x)) => 300 in this case add_num(16, x) is evaluating 16+142 before sending it through. My last piece of advice when you begin to figure out names for your variables and methods is to separate as much of the game logic as you can from the peripheral data. This will make it easier to change things in the long run if you need. puts "\nLets play a guessing game! You have 8 guesses before you lose." target = rand(101) #creates a random number from 0 - 100 attempts = "0".to_i # flag = "8".to_i can be rewritten like so max_user_attempts = 8 @attempt_counter = 0 directions = "\nLets play a guessing game! You have #{max_user_attempts.to_s} guesses before you lose." winning_response = rand(101) def game_loop print directions #.. start loop and call many methods end If you want to change max_user_attempts later you have just one place to change it. or better yet create a method that ask the user if they want 'easy', 'medium', 'hard', 'elite' and set it based on the user response. ( good way to learn about hashes though you can program it other ways. Make easy infinite for responses so you can have the chance to test the functionality of your game without really playing it. sneaking in an easter egg cheat to reveal winning_response is also a good way for testing =) Last note. notice the @ in front of attempt_counter. This allows for methods to see the variable outside their own scope. it's optional if you want to use it but allows for direct manipulation: def increment_attempt @attempt_counter += 1 end vs a more generic approach: counter = 0 def inc_counter( arg) arg += 1 end counter = inc_counter( counter) I hope I haven't confused you. I do not want to make it overcomplicated or obfuscate it so please ask many questions. Methods( and functions) are a very important concept to grok in every programming language in every paradigm. ~Stu