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

Eliza SJ
Eliza SJ
4,587 Points

I'm confused about hashes. Where do the symbols come from and why are they needed here ?

I'm confused about hashes. Where do the symbols come from all of the sudden, and why in this particular case does the example ONLY work w/symbols and not with the other hash notation ?

3 Answers

Kenzo Fry
Kenzo Fry
8,929 Points

Eliza SJ's question is good and I'd like to know the answer too. I don't think symbols are the same as strings with quotes on them. For example, symbols are immutable, so they can't change, but strings can. Jason hasn't explained here why he used symbols to make this hash, and it's not something explained when we went through hashes previously, so it's a little alarming.

At the specific part of Building a bank account Jason mainly uses Arrays. So Ill explain them first in hoping that you are asking about an array.

An Array is a collection of Objects that is accessible with a key, the key being an integer. You create an Array by;

Array.new 

Array ['cat', 'dog', 'mouse', 'parakeet']

Where a Hash is similar to an array with the exception that the keys can be objects.

Hash.new

Hash{'cat' => 'feline', 'dog' => 'canine'}

The specifics are Arrays use the [ square brace define it while a Hash uses the { curly bracket. The stuff in between cat and feline are what defines cat being the key of feline.

Eliza SJ
Eliza SJ
4,587 Points

Thanks Brian. I do understand the difference between arrays and hashes. But I was wondering why in this Treehouse exercise the notation absolutely had to be using symbols instead of the arrows. It's confusing to me that Jason suddenly changes how he's been creating hashes, but never explains why. Nor does he go into detail about what a symbol is. I still don't really understand what a symbol is or how a symbol replaces the hash key :/

Here's a copy and paste of the video transcription when Jason codes for the hash @ transaction.push(description: description, amount: amount)


We'll just say we want a description of the transaction, and an amount. 1:13 And now we can say transactions.push, since it's an array. 1:22 And, we will create a hash here, with a key 1:27 of description, and another key for the amount. 1:35 So now that we have created this add_transaction method,


The symbols are the same things as quotes around the string in hash key. He reviewed this in the hash section when he was used itemize his hashes ( {:name => "Bread" :quantity =>1} ]

hope that helps. I'm struggling along just as well. to make sense of all this

:-)