Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Collections Build a Grocery List Program Build a Grocery List Program: Part 3

no 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:inblock in print_list' from ./shopping_list.rb:26:in each' from ./shopping_list.rb:26:inprint_list' from ./shopping_list.rb:42:in `<main>'

1 Answer

The 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:


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.