This forum is now read-only. Please use our new forums at discuss.codecademy.com.

132 points
52a0f7597c82ca59a4000c47_215273604
Submitted by
Stefan Lasiewski
over 5 years ago

Why do all of my examples return `nil`?

I've run through several dozen Ruby exercises today, starting with "Introduction to Ruby" and currently in "Thtrings & Things".

Just about every single example that I have tried returns the word nil at the very end of the example. Should I expect to see nil every single time, or is this a bug? I don't see anyone else in this forum mentioning the same issue, and for most people the nil output seems to be a rare occurrence, or the result of bad code. I hope I am not writing bad code every single time :)

My browser is Firefox 25.0.1 on Apple Mavericks.

Here are two examples, but this applies to dozens of other exercises that I've tried today.

Exercise 5 in "Introduction to Ruby", with the following code:

puts "What's up?"
print "Oxnard Montalvo"
puts "Hello!"

This returns:

What's up?
Oxnard MontalvoHello!
nil

And here's a example from Thtrings & Things, exercise 7. Code:

print "What is your name?"
user_input = gets.chomp
user_input.downcase!

if user_input.include? "s"
    user_input.gsub!(/s/, "th")
    print "Audios, #{user_input}!"
else
    puts "Nothing to do here!"
end

Results in:

What is your name? Stefan
Audios, Stefan!nil

29 votes

permalink

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

produces:

hello
true

Note: back in the old days, Codecademy used a different format for the implicitly displayed last value that you didn’t actually explicitly print; It used to look like this:

hello
==> true

But now, sadly, the two are indistinguishable.

3117 points
Picture
Submitted by
Alex J
over 5 years ago

11 Comments

Db91a53a1179f280840b1c9596b352c3?s=140&d=retro maxsto2_5228e83280ff3315c50005c9_del over 5 years ago

This resolved my questions for a lot of Codecademy lessons. Thank You!

525f1100f10c6069bd005472_773645533 Maryam Sanieian about 5 years ago

puts "/n"
Does not work in my case.
I also tried finishing the Console with "puts" but I still get the nill.
How is a useful value recognized?? I mean what does it mean? sorry if this sounds basic.

Picture Alex J about 5 years ago

It’s "\n", not "/n". Also, a puts statement will still produce a nil, because that’s the value that puts returns. And it has nothing to do with usefulness: any value is displayed.

C59b0ca82473a586c68819da01948efb?s=140&d=retro gabiebee about 5 years ago

Why does putting true at the end of the code make the nil not show up?

Picture Alex J about 5 years ago

Because now true is the last value, and it shows up instead

46a80248283e0666e06956fc6ca41fc4?s=140&d=retro safik2 about 5 years ago

Thanks for the information to help figure out this issue. The question I have is, is this a code academy issue or is this something I will always have to do whenever I use Ruby

E44aa60a1b91f50b3428b1e02aa95a37?s=140&d=retro coyohti about 5 years ago

I have the same question as safik2. Answer, anyone? Buehler?

Picture Alex J about 5 years ago

safik2, coyohti: the value is only shown in an interactive ruby shell such as irb or pry (Google for them). You normally only use them during development. When a Ruby program is written and runs, it no longer shows the last value.

Afdf9fd6fb5b1b7fcd4e207bf1af5b91?s=140&d=retro Nick445 over 4 years ago

p

6bc55bdb13e818098ebea1ee0f627f61?s=140&d=retro Victoria over 4 years ago

Thanks for explanation! It's very annoying, but good to know it's not our doing.

1e9164a5a54003ac247d49c574dc7bfc?s=140&d=retro Jared Krauss almost 4 years ago

What can you input at the end of your code so that the output is literally nothing, so no "true" or "nil"?


2 votes

permalink

NIL

55 points
273c0ce024c801b59838133ee31f6172?s=140&d=retro
Submitted by
Nicholas M
over 3 years ago


0 votes

permalink

Is the n a numerical value, or is it a string or character "n?" It does not seem to be working for me.

65 points
Picture
Submitted by
Bobby Taruc
about 5 years ago

1 Comment

2b5df0735541ba909f1a2a85173b006e?s=140&d=retro Tiago Mendes-Costa about 5 years ago

In this context, n is a character inside a string.

But there is a twist: whenever you see \ (the backslash character) in a string, the next character takes a special meaning. Here, because n is preceded by \, it means "please make the output go to a new line" instead of "please print the n character".

This backslash trick is often used to insert "unprintable" characters, i.e. the ones you don't want to (or can't) type literally in your source code. \t is another common example: it represents a tabulation character.


0 votes

permalink

It is annoying but your doing everything right. It is just an extra code!

56 points
Db352cbaba8d8bc104c025d951c6abae?s=140&d=retro
Submitted by
tristan811
over 4 years ago

1 Comment

54f82eef95e378f1b9004f78_41090277 Lizard Squad about 4 years ago

we are anonymous rwaeiuglf\erijg fip'\hbetrzhgegf


0 votes

permalink

aww yiss lizard squad is back for another top trumps unboxing today we have the janet edition with the ryans winters interactive dlc pack included since janet owes me one
it looks very nice in its original packaging however just like janet says it aint ever until i have had a bite so now i am going the be eating the packaging while the winters assits me.
right beside i have my scuf controller because we are playing game battles i play this to prove I AM THE BEST with base model guns.

125 points
54f82eef95e378f1b9004f78_41090277
Submitted by
Lizard Squad
almost 4 years ago

1 Comment

54f82eef95e378f1b9004f78_41090277 Lizard Squad almost 4 years ago

ps. josh walker is liz squad


-1 votes

permalink

I was wondering - it was happening to me after any Math code I was entering.... I wasn't getting it on all of them though???

663 points
5880d51c854be958e00003be_623323646
Submitted by
Kathy Borne
over 5 years ago


-2 votes

permalink

I included

puts "\n"

at the end of my code and it continues giving nil at the end. Thats annoying! It seems like a bug to me jaja.

154 points
Picture
Submitted by
Natalia Loaiza
about 5 years ago

1 Comment

54f82eef95e378f1b9004f78_41090277 Lizard Squad about 4 years ago

325435trw654365yhg365wjhlykejgrtkyktrgttttthntmrmmnmntrhmnmrkjl;awe5r3q0-w4arfpkjvetrsjh----- in conclusion i am hacking you


-2 votes

permalink

Kind of annoying that right off the bat there is this annoying "nil" problem to deal with. The first lesson which requires nothing from me just gives me "nil". Maybe I'll switch to Python in case this is going to be an annoying issue that I will have to deal with through the entire process. If there is a way to rectify this issue then the author of the lesson should let us know. I want to see my results properly displayed.

EDIT: I switched to FF and have the same problem. I had been using Chrome.

218 points
D600d8e50acc285767346f83b1a749a2?s=140&d=retro
Submitted by
Blake789
about 5 years ago

1 Comment

81da0a65fb1c6be5916ca6bd7f644648?s=140&d=retro lana.desab about 4 years ago

how about u run the fade


-2 votes

permalink

aCL.EWKOGVAUEWYGBLKDJWSAAAAAAAAF\BC

125 points
54f82eef95e378f1b9004f78_41090277
Submitted by
Lizard Squad
about 4 years ago