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 3: Keeping Our Balance

Rich S
Rich S
1,936 Points

@transactions.push(description: description, amount: amount) What's the purpose behind the colons?

What would happen to the code if it was written as: @transactions.push(description, amount)?

1 Answer

Stone Preston
Stone Preston
42,016 Points

it looks like you are passing in a hash as an argument. When hashes are the only or last argument in a method you can omit the {} around them.

this particular call also uses the popular hash syntax using symbols as the keys, followed by the values. this is quicker and easier to read than the hash rocket syntax

another way to call the method would be:

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

in the above call I included the curly braces and used hash rocket syntax.

note that when using the symbol key syntax, the colon goes after the name of the symbol (instead of in front of the symbol like normal), mimicking the syntax of languages like javascript, swift, etc.

What would happen to the code if it was written as: @transactions.push(description, amount)?

it probably would not work since that is not a valid hash. each pair in a hash has a key and value.

Is this valid?

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