What the nil?


#1



Why do I get the word, "nil" in the terminal at the end of executing my arguments?

https://www.codecademy.com/courses/ruby-beginner-en-MxXx5/0/6?curriculum_id=5059f8619189a5000201fbcb


Replace this line with your code.


#2

nil is a special Ruby data type that means "nothing". :grin:


#3

Yes, but why does it keep showing at the end of the compiler? It doesn't seem like there should be any reason for it to appear if my code is otherwise correct.

Thanks,
Brian


#4

This question pops out from time to time since... this course was published. Someone should add the explanation at the beginning of the course or just delete this feature.


Yes, nil stands for nihil, nothing.

@brianb18 This is the common feature of the REPL's (read - eval - print loop) and it's very helpful (in fact).

Let's say that you have a Ruby console in front of you and you simply want to make some calculations (you are a programmer, a calculator is not for you). So you just type (>> is the input indicator):

>> 28**2
=> 784

Oh, you got the result! Ruby returns the value of the last evaluated expression. Great, right? :slight_smile:

But what happens if you want to print out the result to the console?

>> print 28**2
784
=> nil

Ok, we got the result, but why there is a nil? Because this is what the print returns.


In the normal environment, the return value of the last evaluated expression is marked by the => (or similar) indicator.

For clarification -> Codecademy is not a normal environment.


#5

@factoradic thanks for the explanation. I thought it might be a bug or I was not doing something right.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.