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 trialCaitlin Palmer-Bright
11,310 PointsHelp needed pushing a hash to an array in a method
The task is: Now that the add_transaction method exists, make the add_transaction method append a hash to the @transactions array. The hash should have the keys description and amount, as symbols, and values that match the arguments to the method.
This is what I've done - and it's not passing. Any suggestions?
class BankAccount attr_reader :name
def initialize(name) @name = name @transactions = [] end
def add_transaction(decription, amount) @transactions.push(description: description, amount: amount) end
end
class BankAccount
attr_reader :name
def initialize(name)
@name = name
@transactions = []
end
end
1 Answer
Jacob Herrington
15,835 PointsHey Caitlin!
This is a little tricky, but here is what I did:
def add_transaction(description, amount)
@transactions.push({:description => description, :amount => amount})
end
This function uses the .push function to add the hash {:description => description, :amount => amount}
to the already existing @transactions
array. Does that make sense?
Sam Donald
36,305 PointsSam Donald
36,305 PointsFor some reason they've made it so we have to use symbols for the keys in this challenge (r.e. Jacob Herrington's answer above). Maybe mentioning that in the challenge description would be good?