Why every program returns nil


#1

I was just wondering, why every code returns nil at the end?
can anybody explain this doubt?

Thanks,


#2

Not every code return nil. What's returned is what the last instruction gives.

If for example you try this code:

variable = "yolo"

You'll get "yolo" as a return, since variable equates to the string "yolo".

However, if you use this code:

variable = "yolo"
puts "yolo"

You'll get nil as a return, because puts returns nil.


#3

Well, how about in the loop.... like languages.each { |element| puts element }
Why it did not return NIL at end.. why?


#4

I suppose that's because the each method returns the object it was called on at the end of execution.