Skip to content

Instantly share code, notes, and snippets.

@caleon
Created February 15, 2012 11:42
Show Gist options
  • Save caleon/1835228 to your computer and use it in GitHub Desktop.
Save caleon/1835228 to your computer and use it in GitHub Desktop.
Simple creation of new inflections in Rails 3 (or whatever uses ActiveSupport::Inflector)
# In case you want to define a new inflection so you can call something like:
#
# "masochistic son of a gun".leet # => "m450ch1571c 50n 0f 4 9un"
#
# Do the following so that the requisite methods are injected into each relevant
# class/module:
#
# NewInflection.register :leet do |word|
# word.tr('aegilost', '43911057')
# end
#
# and then it is also available via ActiveSupport::Inflector.leet('something').
module NewInflection
def self.register(name, &proc)
inflector = ActiveSupport::Inflector
inflector.send(:define_method, name, &proc)
String.send(:define_method, name, lambda { inflector.send(name, self) })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment