Created
March 28, 2020 19:47
-
-
Save Snarp/a4e91fb6a6a8f03e4b0588a9eb204a21 to your computer and use it in GitHub Desktop.
Replaces all emoji within a string with ":#{NAME_OF_EMOJI}:"
This file contains hidden or 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
require 'unicode/emoji' | |
require 'gemoji' | |
# Replaces all emoji within a string with ":#{NAME_OF_EMOJI}:" | |
# Ex: "here's a sheep emoji 🐑" | |
# => "here's a sheep emoji :sheep:" | |
def replace_emoji(str) | |
str.scan(Unicode::Emoji::REGEX).each do |emoji| | |
emoji_name = Emoji.find_by_unicode(emoji).name | |
puts "#{emoji} => #{emoji_name}" | |
str.gsub!(emoji, ":#{emoji_name}:") | |
end | |
return str | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment