You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
556 B
Ruby

# The singleton pattern is used to ensure that there is only one instance of a
# class and provides global access to that instance. This pattern is useful when
# you want one instance of a class and many different objects need to access it,
# rather than pass the object around, we can make the object global.
class Singleton
def self.new
@instance ||= super
end
end
# Other example
require 'singleton'
class OtherSingleton
include Singleton
# OtherSingleton.new is not allowed
end
# Usage
s1 = Singleton.new
s2 = OtherSingleton.instance