This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by francescalena
over 10 years

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

56 votes

Permalink

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.

points
Submitted by Alex J
over 10 years

16 comments

francescalena over 10 years

Thank you!

lern2code over 10 years

Yes, thanks a ton!

Danika over 10 years

Super helpful, thank you.

Capeorigins about 10 years

Hmm, its not working for me. What do you think I should do?

Grey Elerson about 10 years

Wow. I searched Google for the answer to that question, and your response was in the top 3 results. Nicely done, Alex!

Mr. Byt3z about 10 years

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

Easy_Come_Easy_Go about 10 years

Thanks!(I knew something was missing)

Taylor Finklea about 10 years

Thanks

Ethan C almost 10 years

Thanks so much!

allamoe over 9 years

Cool! :)

isobelroe over 9 years

argh thanks! the nil was really bugging me!!!

AlbertLake over 9 years

THANKS!!!!!!!!!!!!!!!!!!!! The nil was making me mad:( I thought that there was something wrong with my computer

Manuel Alvarado over 9 years

Thanks Alex! This help us so much. I think everybody is asking why the console prints “nil” at the end of the output. Greetings!

jimb0b360 about 9 years

That bug BUGged me too! XD

James Bartol-Laham almost 9 years

Thought I was going crazy lol

Nicholas Wiewiora over 8 years

“And, more importantly, submit a bug report, I simply can’t be bothered to.” xDD

Answer 5415eeae282ae3f0be0020aa

7 votes

Permalink

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. :-\

points
Submitted by RonanFrost
over 9 years

Answer 548dd4bde39efe04ea006ba2

4 votes

Permalink

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).

points
Submitted by Chris Sphinx
over 9 years

Answer 522f17f7abf821e6170002f2

2 votes

Permalink

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.

points
over 10 years

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

2 votes

Permalink

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

points
Submitted by Capeorigins
almost 10 years

Answer 521bed51abf821911b000aaf

1 vote

Permalink

Thanks a million. Really helpfull

points
Submitted by kane.s
over 10 years

Answer 52cdbafb8c1ccc96850007e3

1 vote

Permalink

i tried the “;” and the nil keeps showing up…any other suggestions?

points
Submitted by Patricia Marin
about 10 years

3 comments

Tyler Sharp about 10 years

I agree, I’m in the same boat.

Alex J about 10 years

See my updated answer above

AlbertLake over 9 years

you must use /n to stop the nil

Answer 521b06ef80ff333be6000b8c

0 votes

Permalink

I don’t know. It does it to me as well, it even does it on my Ruby Script interpreter on my computer. I’d like to know how to get rid of it as well. ;)

points
Submitted by kane.s
over 10 years