Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialbfibbs
5,174 Pointsno implicit conversion of nil into String (TypeError)
I am coding along on the tutorial, but I have encountered an error. Would be grateful for any help
def print_list(list) puts "List: #{list['name']}" puts "----"
list["items"].each do |item| # ["items"].each do |item| #Block 29 # for item in ['items'] do puts "Item: " + item['name'] puts "Quantity: " + item['quantity'].to_s puts "---" end end
I get the error below when I run the code, appreciate any directions.
./shopping_list.rb:29:in +': no implicit conversion of nil into String (TypeError)
from ./shopping_list.rb:29:in
block in print_list'
from ./shopping_list.rb:26:in each'
from ./shopping_list.rb:26:in
print_list'
from ./shopping_list.rb:42:in `<main>'
1 Answer
Barb Hawes
7,253 PointsThe part of the error that says "no implicit conversion of nil into String" suggests that you should look at the part of the code that is converting something to a string:
item['quantity'].to_s
A quick web search of 'ruby nil' says that it means nothing is there or, in this case item['quantity'] doesn't have a value.
You might want to check that item['quantity'] is set to a value before you call the print_list method.