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 trialAdrian Yeow
13,866 PointsThe arguments of a method
first, we define this method
def add_transaction(description, amount) @transaction.push(description: description, amount: amount) end
What I dont understand is this
Comparing this method
def credit(descrip, amount) add_transaction(description, amount) end
with this
def debit(description, amount) add_transaction(description, -amount) end
the first method will return an error to me when i sent in the wrong argument, what i dont understand is that i have always thought that any arguments will do as long as this sentence "add_transaction(description, amount)" is correct. can anyone guide me on this? any help will be much appreciated.
3 Answers
Roberto Alicata
Courses Plus Student 39,959 Pointsdef credit(descrip, amount)
add_transaction(description, amount)
end
bankaccount2.rb:9:in credit': undefined local variable or methoddescription' for # (NameError) 0x007fd73290c>
This error is right because when you call add_transaction method you pass the local variable description that is not defined (you used descrip in the parameters list of the credit method)
You must rewrite the code like this:
def credit(description, amount)
add_transaction(description, amount)
end
Andrew Stelmach
12,583 PointsDon't know if this helps, but you have a typo: "-amount", instead of "amount" in your code there. If you copy/pasted this into your post it's probably messing up your code.
Andrew Stelmach
12,583 PointsAh, maybe it's not a typo, come to think of it!
Adrian Yeow
13,866 Pointstake note of this method def add_transaction(description, amount) @transaction.push(description: description, amount: amount) end
and how i got an error when i called add_transaction and pass in the wrong argument like this
def credit(descrip, amount) add_transaction(description, amount) end
this is the error displayed
bankaccount2.rb:9:in credit': undefined local variable or method
description' for #<BankAccount:0x007fd73290c
c28> (NameError)
i know where the issue is, but i just need some explanation on the errors. Thanks!
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsPlease give an example of what you do and what error it displays.