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 Ruby Objects and Classes Build a Bank Account Class Part 2: Transactions, credits, and debits

Jordano Moscoso
Jordano Moscoso
3,104 Points

Ways to push a Hash

Why when i tried this does not work?:

@transactions.push({"description"=>description,"amount"=>amount})

and this works:

@transactions.push(description:description,amount:amount)

Thanks.

3 Answers

The hash should have the keys description and amount, as symbols, and values that match the arguments to the method.

The instruction said that they have to be symbols.

Eliza SJ
Eliza SJ
4,587 Points

I wish he'd explained symbols a bit better... I'm a bit confused as to why the hash has to absolutely be created using symbols. What makes this particular case only work with symbols?

Hi Eliza,

i hope I answered your question on the other post, but the symbol here is set as is, b/c of the way he framed the code @transaction.push(description: description, amount: amount)

In ruby, this returns: {:description => "description", :amount => "amount"}

the symbol works like quotes around string around a hash KEY.