# Why do I keep getting "nil"?

**URL:** <https://www.ruby-forum.com/t/why-do-i-keep-getting-nil/178411>\
**Category:** Ruby\
**Created:** [November 4, 2009, 7:50am UTC](https://www.ruby-forum.com/t/why-do-i-keep-getting-nil/178411 "2009-11-04T07:50:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 4, 2009, 7:50am UTC](https://www.ruby-forum.com/t/why-do-i-keep-getting-nil/178411/1 "2009-11-04T07:50:18Z")

</div>

I had a homework assignment where I had to create a number guessing game  
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…

# Methods

def get\_user\_name()  
puts “What is your name?”  
name = gets().chomp().capitalize()  
end

def num\_eval(guess, number)

if (guess \> 100)  
puts “That’s more than 100! Pick a number between 1 and 100!”  
elsif (guess \< 1)  
puts “That’s less than 1! Pick a number between 1 and 100!”  
elsif (guess \> number)  
puts “Too high! Try again!”  
elsif (guess \< number)  
puts “Too low! Try again!”  
end  
end

def get\_user\_guess(number)

guess = 0  
num\_guess = 0

while (guess != number) do  
puts “Please enter a number.”  
guess = gets().chomp().to\_i()  
puts num\_eval(guess, number)

```
num_guess = num_guess + 1

```

end

puts “Congrats you won!”  
puts “You guessed the secret number in " + num\_guess.to\_s() + "  
tries!”

end

# Variables

play\_game = “yes”  
magic\_number = 0  
num\_guesses = 0  
num\_round = 1  
guess = 0

puts “Welcome to the guessing game!\n”  
name = get\_user\_name  
puts "Hi, " + name + “!”  
puts “You job is to guess the number between 1 and 100”  
puts “I will tell you if your guess is correct, too low or too high.”

while (play\_game.downcase() == “yes”) do  
puts "Time for Round " + num\_round.to\_s() + “!”  
magic\_number = rand(100) + 1  
puts magic\_number

puts get\_user\_guess(magic\_number)

puts “Would you like to play again?”  
play\_game = gets().chomp()

num\_round = num\_round + 1  
end

puts "\nThank you for playing the Ruby Guessing Game, " + name + “!  
Good-bye!”

---

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 4, 2009, 8:11am UTC](https://www.ruby-forum.com/t/why-do-i-keep-getting-nil/178411/2 "2009-11-04T08:11:39Z")

</div>

> Please enter a number.
> 
> My code is below if that helps…

Your code works correctly, it just prints nil because of the following:

def num\_eval(guess, number)

> 

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  
the if  
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()

> puts num\_eval(guess, number)

You can see that you print the value that num\_eval returns, which is  
nil, as  
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.

---

<div class="post-metadata">

**Author:** ![rickbocas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a3d4f5/32.png) [@rickbocas](https://www.ruby-forum.com/u/rickbocas)\
**Post date:** [November 4, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/why-do-i-keep-getting-nil/178411/3 "2009-11-04T14:55:36Z")

</div>

On Wed, Nov 4, 2009 at 2:10 AM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Please enter a number.
> > 
> > elsif (guess \> number)  
> > statement returns the nil that the print statement returns. And the last  
> > You can see that you print the value that num\_eval returns, which is nil, as  
> > 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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
