Skip to content

Instantly share code, notes, and snippets.

@astroza
Last active October 3, 2018 19:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astroza/b2285906f302e4e9483b to your computer and use it in GitHub Desktop.
Save astroza/b2285906f302e4e9483b to your computer and use it in GitHub Desktop.
Nicer Ruby Singleton
class SingletonTest
def data
@data
end
def data=(n)
@data = n
end
@old_new_method = self.method(:new)
def self.new
@instance ||= @old_new_method.call
end
end
a = SingletonTest.new
a.data = 6
b = SingletonTest.new
puts b.data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment