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

Mohammed Ali
Mohammed Ali
11,134 Points

to_s error

I keep getting error when I try to over ride to_s method by defining the method" def to_s ... it keeps throwing error : undefined local variable or method name - NameError! I don't get that since it seems no else seems to have had same issue.. am stuck let me know plz if you know what the issue is.. Thanks

2 Answers

Mohammed Ali
Mohammed Ali
11,134 Points

It works when I do like this, use instance var directly

def to_s "Name: #{@name}, Balance: #{sprintf("%.2f", balance)}" end

but not when I do like this, it's var name that's the issue not the to_s as I thought first.

def to_s "Name: #{name}, Balance: #{sprintf("%.2f", balance)}" end

Mohammed Ali
Mohammed Ali
11,134 Points

got it...it's all good now. I forgot the attr_reader !!