Ruby có module Singleton trong stdlib — include nó để tạo class chỉ có đúng 1 instance, thread-safe:
require 'singleton'
class AppConfig
include Singleton
attr_accessor :debug, :log_level
def initialize
@debug = false
@log_level = :info
end
end
config = AppConfig.instance
config.debug = true
AppConfig.instance.equal?(config) # => true — cùng object
AppConfig.new # => NoMethodError — privateSingleton module dùng Mutex nội bộ để đảm bảo instance chỉ được tạo 1 lần dù nhiều thread cùng gọi lần đầu.
Ứng dụng: config object, connection pool (1 instance), logger dùng chung, registry global. Lưu ý: Singleton là global state — khó test; cân nhắc dependency injection thay thế trong code production.
Ruby ships the Singleton module in its standard library — include it to create a class with exactly one instance, thread-safe out of the box:
require 'singleton'
class AppConfig
include Singleton
attr_accessor :debug, :log_level
def initialize
@debug = false
@log_level = :info
end
end
config = AppConfig.instance
config.debug = true
AppConfig.instance.equal?(config) # => true — same object
AppConfig.new # => NoMethodError — privateSingleton uses an internal Mutex to ensure instance is created only once even when multiple threads call it simultaneously.
Use cases: config objects, shared connection pools, global loggers, registries. Note: Singleton is global state — hard to test; consider dependency injection in production code.