Created
December 3, 2019 23:20
-
-
Save bryanp/10d08dcc810b10c66881e4ab10154d03 to your computer and use it in GitHub Desktop.
Global Instance Pattern
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
require "forwardable" | |
module Inflection | |
class Inflector | |
def pluralize(string) | |
string + "s" | |
end | |
end | |
class << self | |
extend Forwardable | |
# Delegate all supported instance methods to the global instance. | |
# | |
def_delegators :global, *(Inflection::Inflector.instance_methods - Inflection.methods) | |
def global | |
@global ||= Inflector.new | |
end | |
end | |
end | |
# Provide a global, default inflector out of the box: | |
# | |
Inflection.pluralize("bird") | |
# Or build your own: | |
# | |
Inflection::Inflector.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment