Let’s say I have a singleton class like this:
class Settings
include Singleton
def timeout
# lazy-load timeout from config file, or whatever
end
end
Now if I want to know what timeout to use I need to write something like:
Settings.instance.timeout
but I’d rather shorten that to
Settings.timeout
One obvious way to make this work would be to modify the implementation of Settings to:
class Settings
include Singleton
def self.timeout
instance.timeout
end
def timeout
# lazy-load timeout from config file, or whatever
end
end
That works, but it would be rather tedious to manually write out a class method for each instance method. This is ruby, there must be a clever-clever dynamic way to do this.
One way to do it is like this:
If you want more fine grained control on which methods to delegate, then you can use delegation techniques:
On the other side, I recommend using modules if the intended functionality is limited to behavior, such a solution would be: