Ruby


#1



so i've been practicing Ruby. and when i wrote my puts code it wrote additiona "nil" on the screen which i haven't written. could you help me to understand what is that?


#2

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.


nil stands for nihil, nothing.

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.


#3

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