Skip to content

Instantly share code, notes, and snippets.

@EnriqueVidal
Created May 27, 2011 16:39
Show Gist options
  • Save EnriqueVidal/995641 to your computer and use it in GitHub Desktop.
Save EnriqueVidal/995641 to your computer and use it in GitHub Desktop.
Anonymize strings
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