Skip to content

Instantly share code, notes, and snippets.

@bryanp
Created December 3, 2019 23:20
Show Gist options
  • Save bryanp/10d08dcc810b10c66881e4ab10154d03 to your computer and use it in GitHub Desktop.
Save bryanp/10d08dcc810b10c66881e4ab10154d03 to your computer and use it in GitHub Desktop.
Global Instance Pattern
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