Created
February 15, 2012 11:42
-
-
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)
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
# 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