This forum is now read-only. Please use our new forums! Go to forums
Why is all my code printing nil at the end? It's very foreboding.
Why is all my code printing nil at the end? It’s very foreboding.
Answer 521b1e0d548c35a309000eb3
The Ruby console doesn’t always print nil
at the end, but it does automatically show the value of the last statement or expression that is executed in your script. puts
happens to return nil
, so if you do puts "hello"
, you’ll see hello
, followed by nil
. However, if you say x = 5
, you’ll see 5
instead of nil
, because that’s now the value of the last statement.
[edit 2014-03-03] the semicolon trick no longer works. puts "blah";
will show nil
on in the console with or without the semicolon.
Unfortunately, Codecademy broke the styling of console output at some point, so that the stuff you actually print is indistinguishable from the last value that the console shows:
print "hello"
produces
hellonil
Until Codecademy fixes this (which should take no more than 5 minutes by the way, it’s just a matter of adding a couple of lines of CSS), you can only use workarounds:
code output
-------------------------------------------------
print "hello\n" hello
"bye" "bye"
-------------------------------------------------
puts "hello" hello
?z "z"
-------------------------------------------------
p "hello" hello
hello
And, more importantly, submit a bug report, I simply can’t be bothered to.
Answer 5415eeae282ae3f0be0020aa
A year later …. still with the ‘nil’ at the end of [Almost] everything. For someone who learns by poking and prodding at things to see how they work, it is throwing me off. :-\
Answer 548dd4bde39efe04ea006ba2
If this really bugs you this much, all you need is a custom stylesheet. Create a file called “code_academy.css” and write this:
span.log {
visibility: hidden;
}
Then, in your browser of choice, find where you can set a custom stylesheet and select the file you just created.
If you ever leave the Code Academy site, just set the custom stylesheet back to none (although, leaving it on is unlikely to affect anything).
Answer 522f17f7abf821e6170002f2
The Ruby console doesn’t always print nil at the end, but it does automatically show the value of the last statement or expression that is executed in your script and after execution of line it return nil if we put ; interpreter terminates.
1 comments
Every expression and every object in Ruby has a boolean value, which means that every object and every expression must either evaluate to true or to false. This makes true and false rather special values. But, they are not just empty value or simple keywords, just like with everything else, both true and false are Ruby objects.
Answer 5367d33a7c82caa8970001a8
So I was really confused by this too. Even with the great answers above. I did however figure out that it was showing up because I was viewing the code preview from the editor, and when I made a notepad and tried it outside of the editor it didnt print “nil” So long story short: code editors preview always shows nil. lol
Hope this helps
Answer 521bed51abf821911b000aaf
Answer 52cdbafb8c1ccc96850007e3
i tried the “;” and the nil keeps showing up…any other suggestions?
3 comments
I agree, I’m in the same boat.
See my updated answer above
you must use /n to stop the nil
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
16 comments
Thank you!
Yes, thanks a ton!
Super helpful, thank you.
Hmm, its not working for me. What do you think I should do?
Wow. I searched Google for the answer to that question, and your response was in the top 3 results. Nicely done, Alex!
still printing nil for me….
unless 3 > 5 print “Hello”; end
and it will write hello first, then about 2 seconds later, writes nil then it allows me to continue saying its correct
Thanks!(I knew something was missing)
Thanks
Thanks so much!
Cool! :)
argh thanks! the nil was really bugging me!!!
THANKS!!!!!!!!!!!!!!!!!!!! The nil was making me mad:( I thought that there was something wrong with my computer
Thanks Alex! This help us so much. I think everybody is asking why the console prints “nil” at the end of the output. Greetings!
That bug BUGged me too! XD
Thought I was going crazy lol
“And, more importantly, submit a bug report, I simply can’t be bothered to.” xDD