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 trial

Ruby

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Can someone teach me how to combine just the values of keys together to a total?

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" attr_accessor :checkings, :snack, :drink, :balance, :name

def initialize(checkings, balance) @Checkings = checkings @balance = balance

end

def snack(food) total_items = {} puts "How much is #{food} " price = gets.chomp.to_i total_items[food] = price loop do puts "Wanna buy more food?(Yes/No)" answer = gets.chomp case answer when "Yes", "yes", "YES" puts "What other snack you wanna buy?" snack = gets.chomp puts "Im sorry i can't see the lable how much is that?" price = gets.chomp total_items[snack] = price.to_i when "No", "no", "NO" puts "These are the items you are buying." p "----------------------------------------------------" total_items.each_key {|key| puts "You bought #{key}"} p "----------------------------------------------------" puts "Averaging total #{total_items.values}" total_items.values.to_s total_items break end end end end

def reciept(items)
    p "Your total for #{name} is"
    items.value


ally_bank = Bank.new("Chase", 120)
ally_bank.snack("Chips")
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""'
What i wanna do in reciept  is factor in all the snacks that ally bought and add the total values of the items together.
Caitlin Palmer-Bright
Caitlin Palmer-Bright
11,310 Points

Hard to read the code above your receipt method, but I would suggest

  1. turning the values of the hash into an array using the total_items.map method
  2. Using the inject method to sum the values from that array together.

Have a look at the enumerable course for help on this :)