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 Adding Transactions

Why doesnt writing a hash like this work

This should be the exact same thing as what is occurring in the video

  def add_transaction(description, amount)
    @transactions.push({"description" => description, "amount" => amount})
  end
bank_account.rb
class BankAccount
  attr_reader :name

  def initialize(name)
    @name = name
    @transactions = []
  end

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

end

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

Pay attention to what they challenge is asking you to do here. It wants you to use symbols for the keys, but you're using strings. Otherwise, your code should work

Why dont you need the curly brackets then?

Michael Hulet
Michael Hulet
47,913 Points

In Ruby, when you're passing a hash literal into a method, you don't need to write the curly brackets around a hash literal, because Ruby can tell from your use of the => operator or symbols with the colon on the right side that you mean to have a hash literal there. It's just one of the cool syntactical sugar features of the language