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

Justin Sze Wei Teo
Justin Sze Wei Teo
9,418 Points

Question on transaction[:amount] , accessing value inside a hash

Hi guys,

I refer to the code in this video as follows,

def balance
    balance = 0.0
    @transactions.each do |transaction|
      balance += transaction[:amount]
    end

To provide some context, the instance variable @transactions is an array of hashes , where each hash consists of a few key-value pairs ,one of which is {...., amount: amount,..... }.

From my understanding, the code "transaction[:amount]" is looping over the array of hashes, and accessing the "amount" key-value pair, to obtain its value.

I looked through past ruby hash notes but couldn't find any of this.

Does this example imply that "hash[:key]" is a method to access the value of a particular key in the hash?

Can't seem to find this in ruby documentations as well (or perhaps I'm just not adept at navigating around the ruby site).

Any ideas anyone?

4 Answers

Jorge Rodriguez
Jorge Rodriguez
2,616 Points

Hello Justin.

Hashes are indexed using the square brackets ([]). Just as arrays. But instead of indexing with the numerical index, hashes are indexed using either the string literal you used for the key, or the symbol. So if your hash is similar to: hash = { :key1 => "value1", :key2 => "value2"} or hash = { key1: "value1", key2: "value2" } you can access a value like this: hash[:key1]

Mars Epaecap
Mars Epaecap
5,110 Points

Thanks Jorge for explaining that! Does the method

hash[:key1]

only work if your hash is in this format?

 hash = { key1: "value1", key2: "value2" }

if so how would you access a value only if your hash was in the other format

hash = { :key1 => "value1", :key2 => "value2"}
Jorge Rodriguez
Jorge Rodriguez
2,616 Points

Hello

Any format of a hash should work with the " access method" '''hash[:key1]'''

You can try it in irb.

Greetings Mars!

Hi Jorge,

Instead of [:amount], I was wondering why the hash value cannot be accessed by using ["amount"] in this case?

Jorge Rodriguez
Jorge Rodriguez
2,616 Points

Hello dch

Because you are trying to access the key value and there is only 2 options for accessing, listed above. For example, if I want to access the key value by using: ["amount"] it will happen the following in my IRB: irb(main):001:0> hash = { key1: "value1", key2: "value2" } => {:key1=>"value1", :key2=>"value2"} irb(main):002:0> hash[:key1] => "value1" irb(main):003:0> hash["key1"] => nil