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 trialSpencer Ruiz
2,262 PointsMy Attempt At The Grocery List *Extra Credit*
I spent a good four to five hours working on this. Between hashes not pushing and loops not looping I thought I was going to go insane. I used a global variable to remove the question from the list_name method to stop the program from asking the name of the grocery list twice. I think I read somewhere global variables are a bad idea but I couldn't think of another way to do it. Let me know what you think!
#this program is a loop that asks for items to retun a list with a title and quantites
print "Please title your list: "
@name = gets.chomp
def list_name
list_name = @name
hash = { "name" => list_name, "items" => Array.new }
return hash
end
def add_item
print "What item would you like to add?: "
item = gets.chomp
print "How much?: "
quantity = gets.chomp.to_i
hash = { "item" => item, "quantity" => quantity }
return hash
end
list = list_name
def print_list(list)
print "List Title: #{list_name['name']}\n\n"
list["items"].each do |item|
print "Item: " + item['item']
print "\t\t\t"
print "Quantity: " + item['quantity'].to_s + "\n"
end
end
def continue_list()
print "Would you like to add an item? (Y/N): "
continue = gets.chomp.upcase
if continue == "Y"
return true
else
return false
end
end
while continue_list() == true do
list["items"].push(add_item())
end
puts "\n\nHere is your complete list!\n\n"
print_list(list)
2 Answers
Jacob Moyle
6,150 PointsHi Spencer,
Like Daniel Cunningham said, there are a lot of return statements. Remember, Ruby has implicit returns.
Daniel also has a cool idea of using a class. It would be cool to see a 'runner' loop containing a case statement that allows a user to call the various methods that have been created. Just a thought!
Cheers, Jacob
ben leonard
1,096 PointsHere's my take on this :)
def list_name
puts "What do you want to call your list? "
name = gets.chomp
hash = {:name => name, :items => Array.new}
return hash
end
def list_items
puts "What item do you need? "
item = gets.chomp
puts "How many do you need? "
quantity = gets.chomp.to_i
hash = {:item => item, :quantity => quantity}
return hash
end
def seperator(character='-')
print character * 80
end
list = list_name
list[:items].push(list_items)
def list_output(list)
list[:items].each do |item|
puts "\tItem: " + item[:item] + "\t\t\t" +
"Quantity: " + item[:quantity].to_s
seperator()
end
end
def list_loop(list)
puts "Do you want to add another item, yes or no? "
answer = gets.chomp.downcase
if answer == "yes"
list[:items].push(list_items)
list_loop(list)
else
list_output(list)
end
end
list
list_loop(list)
list_output(list)
Daniel Cunningham
21,109 PointsDaniel Cunningham
21,109 PointsIt looks like it works, but there are a lot of return statements... Just to share, I took a stab at this myself and came up with this.
I made a Class out of this and called an instance of the class. Turning it into a class isnt necessary at all, but I wanted to experiment with it. In this case, create_list() was replaced by an "initialize" method that will create the attributes and the name for the list. Then it has the following elements:
another way to clean up my code might be to put the add_item_check code directly into the add_list_item method, but this works for now.