On Wed, Nov 4, 2009 at 2:10 AM, Josh Cheek <josh.cheek / gmail.com> wrote: >> >> using custom methods. I got it to work...but after some lines it'll put >> "nil" on the next line and continue with the program. >> >> ex. after an input >> >> Please enter a number. >> 4 >> nil >> Too low. Try again. >> Please enter a number. >> 7 >> nil >> Too low. Try again. >> >> or after a generated line >> >> You guessed the secret number in X tries! >> nil >> >> Why is it saying "nil" everywhere and how do I stop it? >> >> My code is below if that helps... >> >> > Your code works correctly, it just prints nil because of the following: > > def num_eval(guess, number) >> ¨Β¨ηυεσσ ±°°© >> ¨ΒυτΆΤθατ§νοςε τθαξ ±°°΅ Πιγλ ξυνβες βετχεεαξ±°°΅>> ¨Βμσιζ ¨ηυεσσ ±© >> ¨ΒυτΆΤθατ§μεσσ τθαξ ±΅ Πιγλ ξυνβες βετχεεαξ±°°΅>> ¨Βμσιζ ¨ηυεσσ ξυνβες>> ¨ΒυτΆΤοο θιηθΤςαηαιξ΅>> ¨Βμσιζ ¨ηυεσσ ξυνβες>> ¨ΒυτΆΤοο μοχ΅ Τςαηαιξ΅>> ¨Βξδ >> end >> >> > That is how your num_eval function is declared. Each of those if statements > prints to the screen, but the print statement itself returns nil. And thef > statement returns whatever the last line in it's scope returns, so the if > statement returns the nil that the print statement returns. And the last > returned value in the function is what the function returns. So the if > statement returns nil, and that goes out of the function as the return > value. > > Then, where you actually use the code below: > > guess = gets().chomp().to_i() >> ¨Βυτξυνίεφαμ¨ηυεσσξυνβες>> > > You can see that you print the value that num_eval returns, which is nil,s > described above. > > So, your program works (at least for my 2 trials), but it just prints nil > because num_eval returns nil, and you are printing it's value. > And the solution is either to 1) not puts the result of nil or 2) which I prefer, remove all the puts'eses from num_eval so that it returns the evaluation as a string: def num_eval(guess, number) if (guess > 100) "That's more than 100! Pick a number between 1 and 100!" elsif (guess < 1) "That's less than 1! Pick a number between 1 and 100!" elsif (guess > number) "Too high! Try again!" elsif (guess < number) "Too low! Try again!" end end -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale