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 4

Grace Kelly
Grace Kelly
33,990 Points

Ruby Collections Extra Credit

Hey guys I just finished the Ruby Collections course which was awesome!! I did the extra credit and thought I'd share my solution. Instead of a shopping list, I created a simple Pokemon Team Builder with while loops and conditionals, Here's a snapshot of my code: https://w.trhou.se/elsja2kuz9

I'm sure there is a much more elegant way of doing this and I'll probably come back to it once I've learned more Ruby, but this extra credit exercise really helped me retain what I learned on the course...now onwards to Ruby Loops!!

Maurice Tafolla- Cunningham
Maurice Tafolla- Cunningham
7,708 Points

I am concerned with this bit of code:

#Print Team  
def print_team(team) 

  team_seperator()
  puts "Team: #{team["name"]}\n"

  team["members"].each do |member|
    puts "\tPokemon: " + member['name'] + "\t\t\t" + 
          "\tType: " + member['type']

  end
  team_seperator()
end

Where is the statement that should read "team = __________________"?

I have no idea where you are interpolating the value for ["name"] when you type:

 puts "Team: #{team["name"]}\n" 

I am really confused because you do not have a "team" variable anywhere in the code and it works just the same as the exercise in the video.

For example in the video they have code that looks like this:

def print_list(list)
  puts "List: #{list['name']}"
  print_separator()

  list["items"].each do |item|
    puts "\tItem: " + item['name'] +"\t\t\t" +
         "Quantity: " + item['quantity'].to_s

    end
    print_separator()
end

list = create_list()

list = create_list() <------ this is what I am looking for in your code.

Grace Kelly
Grace Kelly
33,990 Points

Hi Maurice, the "team" value that you are seeing is an argument that I am passing into the print_team method, it's kind of like a placeholder as it's telling the method what to expect. I could have called this argument anything like treehouse or mike but i wanted to name it something relevant to what I was doing. I don't have to declare this team argument value as I will be passing my pokemon_team variable inside the method. This is the process:

I created a method that creates my team, this gives me a hash with the keys "name", "quantity" and "members"

def create_team(team_name, quantity)
  hash = {"name" => team_name, "quantity" => quantity, "members" => []}
  return hash
end

I create the team variable using this code:

pokemon_team = create_team(team_name, quantity)

Later on in the code I push the pokemon team members into the "members" array, but let's ignore that for now.

The print_team method takes an argument e.g the pokemon_team hash and prints out the information:

#Print Team  
def print_team(team) 

  team_seperator()
  puts "Team: #{team["name"]}\n" //outputs the team's name (pokemon_team["name"]) 

  team["members"].each do |member|
    puts "\tPokemon: " + member['name'] + "\t\t\t" +  
          "\tType: " + member['type']

  end
  team_seperator()
end

and then I pass it into the print_team method as the "team" argument:

print_team(pokemon_team)

Hope that clears things up a bit :)

2 Answers

Well done!

I wonder why Treehouse doesn't teach TDD though?

Philip Bessa
Philip Bessa
5,396 Points

Two months late, but...

They do, in the Rails Development Track. I highly recommend you avoid it entirely though.

David O' Rojo
David O' Rojo
11,050 Points

Following TDD principles, I included tests cases with my program using MiniTest. As at this course we are no using classes and objects, I had to resort to monkey-patch the Kernel module and also created a couple of new methods to make the testing easier.

If someone wants to look at it, this is the URL: https://teamtreehouse.com/workspaces/21256972