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

steven alston
steven alston
16,292 Points

in `log': undefined method `add' for nil:NilClass

is this code deprecated in the current version of Ruby. Here is my code

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 "Withdrawing #{amount}" ###logger.debug
        @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("Steven")
account.deposit(250)
account.withdraw(75)

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

It works fine in every version above 1.9 if you change line 9 to this:

@stdout_logger = Logger.new(STDOUT)