Created
May 27, 2011 16:39
-
-
Save EnriqueVidal/995641 to your computer and use it in GitHub Desktop.
Anonymize strings
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
def anonymize(value, options={}) | |
options[:percent] ||= 0.6 | |
options[:percent] = options[:percent] * 0.01 if options[:percent] > 1 | |
options[:char] ||= '*' | |
value_length = value.split( options[:split_by] ).first.length if options[:split_by] | |
value_length ||= value.length | |
obfuscated_length = ( value_length * options[:percent] ).floor | |
start_at = (( value_length - obfuscated_length) / 2).ceil - 1 | |
value.sub(value[start_at..(obfuscated_length)], options[:char] * obfuscated_length) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment