Skip to content

Instantly share code, notes, and snippets.

@sidonath
Last active August 29, 2015 14:01
Show Gist options
  • Save sidonath/f6648c1883f0d956eb81 to your computer and use it in GitHub Desktop.
Save sidonath/f6648c1883f0d956eb81 to your computer and use it in GitHub Desktop.
Why is it bad to call instance method in initializers
class Logger
def info(*args)
puts(*args)
end
end
class Auditor
def log(action)
puts "Following action was performed: #{action}"
end
end
class Destroyer
attr_accessor :logger
def initialize(logger)
self.logger = logger
end
end
class AuditedDestroyer < Destroyer
attr_accessor :auditor
def initialize(logger, auditor)
super(logger)
self.auditor = auditor
end
def logger=(value)
super(value)
auditor.log("Log assigned")
end
end
AuditedDestroyer.new(Logger.new, Auditor.new)
# => initialize-instance-method-example.rb:31:in `logger=': undefined method `log' for nil:NilClass (NoMethodError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment