Skip to content

Instantly share code, notes, and snippets.

@0x0dea
Created September 21, 2015 03:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x0dea/11c33823029559ba270f to your computer and use it in GitHub Desktop.
Save 0x0dea/11c33823029559ba270f to your computer and use it in GitHub Desktop.
sentence = "For now you ask why."
hash = {
FOR: "4",
YOU: "U",
WHY: "Y",
}
# Start with an upcased copy of the original.
slang = sentence.upcase
hash.each do |word, alt|
# Use gsub! (note the bang) to modify the string in-place.
slang.gsub!(/\b#{word}\b/, alt)
end
puts slang
sentence = "For now you ask why."
hash = {
"FOR" => "4",
"YOU" => "U",
"WHY" => "Y",
}
hash.default_proc = -> k, v { v }
puts sentence.upcase.gsub /\w+/, hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment