Last active
August 29, 2015 14:11
-
-
Save liaden/8543292ac7a02a2fb7fc to your computer and use it in GitHub Desktop.
because singletons are bad
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
# before | |
class X | |
# imperative, affects system state, depends on globals | |
def self.x | |
puts Config.data | |
end | |
end | |
X.x | |
# after | |
require 'active_support/core_ext' | |
class X | |
# make data local | |
def initialize(data = Config.data) | |
@data = data | |
end | |
# use local data | |
def x | |
puts @data | |
end | |
# temporary shim to support old code invocations | |
class << self | |
delegate :x, :to => :new | |
end | |
end | |
X.new.x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment