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 trialJoy McCaffrey
3,333 PointsRuby Objects and Classes Transactions, Credits and Debits
I'm following along with the video in Sublime and Ruby, and when I run the program I get this error message:
ruby bank_account.rb
bank_account.rb:11:in add_transaction': undefined method
push' for nil:NilClass (NoMethodError)
from bank_account.rb:25:in `<main>'
Here's my code:
class BankAccount
attr_reader :name
def initialize(name)
@name = name
@transations = []
end
def add_transaction(description, amount)
@transactions.push(description: description, amount: amount)
end
end
bank_account = BankAccount.new("Joy")
bank_account.add_transaction("Groceries", 40)
Why in the world is it not recognizing the push method?
Moderator Edited: Added markdown to the code block so it is readable in the Community Forum... Please refer to the markdown cheatsheet for posting code to the forums
Bobby Ross
9,067 Pointsdef initialize(name) @name = name @transations = []
I think the typo above is causing your issue, this should be @transactions rather than @transations
1 Answer
Joy McCaffrey
3,333 Points@bobbyross Oh my god, that was it! I can't believe it was just a stupid typo! Thank you!
Bobby Ross
9,067 Pointsno problem, I have had countless issues caused by a small typo already, part of the learning curve!
happy coding!
MICHAEL P
5,191 PointsMICHAEL P
5,191 PointsI think it may be, because you have (def add_transaction) and underneath that, you have @transactions.push .Further down, you have bank_account.add_transaction, on the very bottom line.
So, I think that you have to maintain consistency as far as whether or not you want to have (transaction, or transactions) ; and than stick with the same form.
As far as I can tell, you did the actual coding correctly, but there are some small typos.