Last active
August 29, 2015 14:01
-
-
Save sidonath/f6648c1883f0d956eb81 to your computer and use it in GitHub Desktop.
Why is it bad to call instance method in initializers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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