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 Foundations Ruby Standard Library Logger

gerald blady
gerald blady
9,052 Points

name error

getting the following error message.. it's regarding account = BankAccount.new('my name')

the error message is

(NameError) from logging-example.rb:30:in new' from logging-example.rb:30:in<main>'

require 'logger'

class BankAccount attr_reader :file_logger, :stdout_logger

def initialize(name)
    @name = name
    @transactions = []
    @logger = logger.new(STDOUT)
    @file_logger = Logger.new("./bank_account.log")
end

def deposit(amount)
    log "depositing #{amount}"
    @transactions.push(amount)
end

def withdraw(amount)
    log "withdraring #{withdraw}"
    @transactions.push(-amount)
end

def log(message, level=Logger::INFO)
    file_logger.add level, message, "#{self.class} (#{name})"
    stdout_logger.add level, message, "#{self.class} (#{name})"
end

end

account = BankAccount.new('my name') account.depost(100) account.withdraw(50)

6 Answers

steven alston
steven alston
16,292 Points

you forgot to add the @ symbol

file_logger.add level, message, "#{self.class} (#{@name})" stdout_logger.add level, message, "#{self.class} (#{@name})"

Sophie, you're missing a ':', change the line to attr_reader :file_logger, :stdout_logger

Your @logger property should be accessing the Logger class (i.e. beginning with a capital 'L').

Change that and see how you go...

gerald blady
gerald blady
9,052 Points

that was a small error but doesn't seem to be what i'm looking for.

Sorry, you also haven't changed the @logger property to @stdout_logger as Jason described in the video.

gerald blady
gerald blady
9,052 Points

gonna have to rewatch and type out tomorrow. appreciate the swift reply

Sophia Harrison
Sophia Harrison
4,534 Points

I have the exact same issue minus the typo issues :

require 'logger'

class BankAccount
   attr_reader :file_logger, stdout_logger

   def initialize(name)
     @name = name
     @transactions = []
     @stdout_logger = Logger.new(STDOUT)
     @flie_logger = Logger.new("./bank_account.log")
   end

   def deposit(amount)
     log "Depositing #{amount}"
     @transactions.push(amount)
   end

   def withdraw(amount)
     log "Withdrawing #{amount}"
     @transactions.push(-amount)
   end

   def log(message, level=Logger::INFO)
     file_logger.add level, message, "#{self.class} (#{@name})"
     stdout_logger.add level, message, "#{self.class} (#{@name})"
   end
end

account = BankAccount.new("Sophia Harrison")
account.deposit(100)
account.withdraw(50)