Why always a word showing up in the preview "nil"


#1



Replace this line with your code.


#2

nil is the Ruby object that represents nothingness. Whenever a method doesn’t return a useful value, it returns nil. puts and print are methods that return nil:

result = puts "hello"           #=> hello
result == nil                   #=> true

Since the Ruby Console always shows the value of the last statement or expression in your code, if that last statement is print, you’ll see the nil.

To prevent the nil from "sticking" to the output of print (which doesn’t insert a line break), you can print a line break after it, and optionally put some other value as the last statement of your code, then the Console will show it instead of nil:

print "hello"
puts "\n"
true

#3

thank you for clarification, its rolling on my mind till i started with the ruby


#4

You're welcome, happy coding! :grin:


#5

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