Last active
October 3, 2018 19:55
-
-
Save astroza/b2285906f302e4e9483b to your computer and use it in GitHub Desktop.
Nicer Ruby Singleton
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 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