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

Why and how are symbols used as keys inside the add_transaction method?

I don't remember where any of the videos explained exactly what the purpose of a symbol was, so I'm not quite sure how they are used.

For example, what difference would it make if the keys "description" and "amount" were used instead of :description and :amount? And what difference does it make if the colon is a prefix or a suffix?

brian biggers
brian biggers
8,556 Points

This wasn't explained very well. He was using strings as keys up until this point and he gives no explanation as to why he starts using symbols all of a sudden. Smh

1 Answer

In Ruby, symbols are immutable strings, written with a colon prefix, like :symbol. They are used to save memory, for faster running applications.
When creating a hash, if the keys are not explicitly made "strings", they default to symbols like in the video.
The colon afterwards is for assignment, rather than =>

Learn more from this Stack Overflow post (and the answer's included links):
https://stackoverflow.com/questions/8189416/why-use-symbols-as-hash-keys-in-ruby