Skip to content

Instantly share code, notes, and snippets.

@Snarp
Created March 28, 2020 19:47
Show Gist options
  • Save Snarp/a4e91fb6a6a8f03e4b0588a9eb204a21 to your computer and use it in GitHub Desktop.
Save Snarp/a4e91fb6a6a8f03e4b0588a9eb204a21 to your computer and use it in GitHub Desktop.
Replaces all emoji within a string with ":#{NAME_OF_EMOJI}:"
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