Skip to content

Instantly share code, notes, and snippets.

@liaden
Last active August 29, 2015 14:11
Show Gist options
  • Save liaden/8543292ac7a02a2fb7fc to your computer and use it in GitHub Desktop.
Save liaden/8543292ac7a02a2fb7fc to your computer and use it in GitHub Desktop.
because singletons are bad
# 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