### Install Gemoji dependency
Source: https://github.com/github/gemoji/blob/master/README.md
Add the gemoji library to your Ruby on Rails project by including it in your Gemfile.
```ruby
gem 'gemoji'
```
--------------------------------
### Handle Skin Tone Support with Gemoji
Source: https://context7.com/github/gemoji/llms.txt
Illustrates how Gemoji handles Fitzpatrick scale skin tone modifiers. It shows how to check if an emoji supports skin tones, retrieve all skin tone variants, and find the base emoji from a skin-toned variant. This is useful for displaying emojis with appropriate skin colors.
```ruby
require 'gemoji'
# Check if emoji supports skin tones
wave = Emoji.find_by_alias("wave")
puts wave.skin_tones? # Output: true
smiley = Emoji.find_by_alias("smiley")
puts smiley.skin_tones? # Output: false
# Get all skin tone variants
variants = wave.raw_skin_tone_variants
puts "Wave variants count: #{variants.size}" # Output: 5
variants.each_with_index do |variant, i|
hex = Emoji::Character.hex_inspect(variant)
puts "Variant #{i + 1}: #{hex}"
end
# Output:
# Variant 1: 1f44b-1f3fb (light skin)
# Variant 2: 1f44b-1f3fc (medium-light skin)
# Variant 3: 1f44b-1f3fd (medium skin)
# Variant 4: 1f44b-1f3fe (medium-dark skin)
# Variant 5: 1f44b-1f3ff (dark skin)
# Find base emoji from skin tone variant
dark_wave = Emoji.find_by_unicode("\u{1f44b}\u{1f3ff}")
puts dark_wave.name # Output: wave (returns base emoji)
# People holding hands - skin tone applied to both
holding_hands = Emoji.find_by_alias("people_holding_hands")
puts holding_hands.skin_tones? # Output: true
puts holding_hands.raw_skin_tone_variants.size # Output: 5
```
--------------------------------
### Style Emoji Preview Interface with CSS
Source: https://github.com/github/gemoji/blob/master/db/index.html
Defines the visual layout for the emoji list, including font settings, spacing, and styling for emoji aliases and tags. It ensures consistent rendering of the emoji preview container.
```css
body { font: 14px/18px Verdana, Arial, sans-serif; padding: 2em; }
ol { list-style: none; padding-left: 0; }
li { margin-left: 0; margin-top: 5px; clear: left; }
li:first-child { display: none; }
li > span { display: block; }
.emoji { font-size: 50px; line-height: 50px; float: left; margin-right: 20px; }
.description { font-style: italic; color: gray; }
.aliases span:before, .aliases span:after { content: ":"; color: gray; }
.tags { font-size: 11px; }
.tags span { display: inline-block; padding: 1px 5px 2px; border-radius: 3px; background: #dadada; line-height: 11px; }
```
--------------------------------
### Render and Search Emojis with JavaScript
Source: https://github.com/github/gemoji/blob/master/db/index.html
Handles fetching emoji data via XMLHttpRequest, dynamically generating DOM elements for the list, and implementing a debounced search function to filter emojis based on user input.
```javascript
function renderEmoji(emojis) {
var item, els, template = document.querySelector('li');
for (var emoji, i=0; i < emojis.length; i++) {
emoji = emojis[i];
item = template.cloneNode(true);
els = item.querySelectorAll('span');
if (emoji.emoji) els[0].textContent = emoji.emoji;
else {
var img = document.createElement('img');
img.src = "../images/" + emoji.aliases[0] + ".png";
els[0].appendChild(img);
}
els[1].textContent = emoji.description || '';
els[2].innerHTML = emoji.aliases.map(function(n){ return ''+n+'' }).join(' ');
els[3].innerHTML = emoji.tags.map(function(n){ return ''+n+'' }).join(' ');
template.parentNode.appendChild(item);
item._emojiText = (els[2].textContent + ' ' + els[3].textContent).replace(/_/g, '-');
}
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
var json = JSON.parse(this.responseText);
renderEmoji(json);
}
};
xhr.open('GET', 'emoji.json', false);
xhr.send(null);
function searchEmoji(query) {
var el, re = new RegExp('\\b' + query), els = document.querySelectorAll('li');
for (var i=1; i < els.length; i++) {
el = els[i];
if ( !query || re.test(el._emojiText) ) {
el.style.display = 'list-item';
} else {
el.style.display = 'none';
}
}
}
var timeout, search = document.querySelector('input[type=search]');
search.addEventListener('input', function() {
var $this = this;
if (timeout) clearTimeout(timeout);
timeout = setTimeout(function(){
searchEmoji($this.value);
}, 200);
}, false);
search.focus();
```
--------------------------------
### Create Custom Emojis with Emoji.create
Source: https://context7.com/github/gemoji/llms.txt
Define and register a new custom emoji. This method accepts a name and a configuration block to set aliases, Unicode mappings, and metadata tags.
```ruby
require 'gemoji'
# Create a custom emoji with Unicode alias
music_emoji = Emoji.create("music") do |char|
char.add_alias "song"
char.add_alias "melody"
char.add_unicode_alias "\u{266b}"
char.add_tag "notes"
char.add_tag "sound"
end
puts music_emoji.name
puts music_emoji.aliases
puts Emoji.find_by_alias("song").name
```
--------------------------------
### Rails Helper for Emoji Conversion
Source: https://context7.com/github/gemoji/llms.txt
Provides Ruby code for Rails helpers to convert emoji aliases (like `:wave:`) into `
` tags for display, and vice versa, converting raw Unicode emojis into their alias format for storage. This facilitates seamless emoji integration within Rails applications.
```ruby
# app/helpers/emoji_helper.rb
module EmojiHelper
# Convert :alias: syntax to emoji images
def emojify(content)
return nil unless content.present?
h(content).to_str.gsub(/:([\w+-]+):/) do |match|
if emoji = Emoji.find_by_alias($1)
image_tag(
"emoji/#{emoji.image_filename}",
alt: $1,
class: "emoji",
width: 20,
height: 20,
style: "vertical-align:middle"
)
else
match # Return original if no emoji found
end
end.html_safe
end
# Convert raw Unicode emojis to aliases for storage
def emoji_to_alias(content)
return nil unless content.present?
content.gsub(/[\u{1f300}-\u{1f9ff}]/) do |char|
if emoji = Emoji.find_by_unicode(char)
":#{emoji.name}:"
else
char
end
end
end
end
# Usage in views:
# <%= emojify("Hello :wave: Welcome! :smile:") %>
# Output: Hello
Welcome!
# <%= emoji_to_alias("Hello 👋 Welcome! 😄") %>
# Output: Hello :wave: Welcome! :smile:
```
--------------------------------
### Implement Rails Emoji Helper
Source: https://github.com/github/gemoji/blob/master/README.md
A helper method to automatically convert emoji aliases (e.g., :cat:) into HTML image tags within your Rails views.
```ruby
module EmojiHelper
def emojify(content)
h(content).to_str.gsub(/:([\w+-]+):/) do |match|
if emoji = Emoji.find_by_alias($1)
%(
)
else
match
end
end.html_safe if content.present?
end
end
```
--------------------------------
### Create Custom Emoji with Gemoji
Source: https://context7.com/github/gemoji/llms.txt
Demonstrates how to create a custom emoji that is not based on Unicode characters. It involves providing a unique name and specifying an image filename for rendering. The custom emoji can be identified using the `custom?` method.
```ruby
require 'gemoji'
custom = Emoji.create("company_logo") do |char|
char.add_tag "brand"
char.image_filename = "custom/company_logo.png"
end
puts custom.custom? # Output: true
puts custom.image_filename # Output: custom/company_logo.png
```
--------------------------------
### Create and Manage Custom Emojis
Source: https://github.com/github/gemoji/blob/master/README.md
Define new emojis or modify existing ones by adding aliases, tags, and Unicode mappings. Custom emojis without Unicode aliases are marked as custom and require specific image assets.
```ruby
emoji = Emoji.create("music") do |char|
char.add_alias "song"
char.add_unicode_alias "\u{266b}"
char.add_tag "notes"
end
# Custom emoji example
emoji = Emoji.create("music") do |char|
char.add_tag "notes"
end
# Customizing filename
emoji = Emoji.create("music") do |char|
char.image_filename = "subdirectory/my_emoji.gif"
end
# Editing existing
emoji = Emoji.find_by_alias "musical_note"
Emoji.edit_emoji(emoji) do |char|
char.add_alias "music"
char.add_unicode_alias "\u{266b}"
char.add_tag "notes"
end
```
--------------------------------
### Build Emoji Lookup Table and Convert Formats in Ruby
Source: https://context7.com/github/gemoji/llms.txt
Provides utility functions to generate a comprehensive emoji map and perform bidirectional conversions between aliases, Unicode, and hex strings. These functions rely on the Gemoji library to access emoji metadata and character representations.
```ruby
require 'gemoji'
# Build a complete emoji lookup table
def build_emoji_map
map = {}
Emoji.all.each do |emoji|
emoji.aliases.each do |name|
map[name] = {
raw: emoji.raw,
hex: emoji.hex_inspect,
category: emoji.category,
description: emoji.description
}
end
end
map
end
# Convert between formats
def alias_to_unicode(alias_name)
emoji = Emoji.find_by_alias(alias_name)
emoji&.raw
end
def unicode_to_hex(unicode_char)
emoji = Emoji.find_by_unicode(unicode_char)
emoji&.hex_inspect
end
def hex_to_unicode(hex_string)
codepoints = hex_string.split('-').map { |h| h.to_i(16) }
codepoints.pack('U*')
end
```
--------------------------------
### Access Emoji Properties with Gemoji
Source: https://context7.com/github/gemoji/llms.txt
Details the properties available through the `Emoji::Character` class. This includes accessing the emoji's name, raw representation, aliases, category, description, version information, and image filename. It also shows how to check if an emoji is custom and how to inspect its hex representation.
```ruby
require 'gemoji'
emoji = Emoji.find_by_alias('family_man_woman_girl')
# Basic properties
puts emoji.name # Output: family_man_woman_girl
puts emoji.raw # Output: 👨👩👧
puts emoji.aliases # Output: ["family_man_woman_girl"]
# Metadata
puts emoji.category # Output: People & Body
puts emoji.description # Output: family: man, woman, girl
puts emoji.unicode_version # Output: 6.0
puts emoji.ios_version # Output: 8.3
# Tags for searching
puts emoji.tags # Output: []
# Image filename for rendering
puts emoji.image_filename # Output: unicode/1f468-1f469-1f467.png
# Check if it's a custom (non-Unicode) emoji
puts emoji.custom? # Output: false
# Hex inspection for debugging
puts emoji.hex_inspect # Output: 1f468-200d-1f469-200d-1f467
# Static hex inspection method
puts Emoji::Character.hex_inspect("🐱") # Output: 1f431
```
--------------------------------
### Translate Emoji Names and Unicode
Source: https://github.com/github/gemoji/blob/master/README.md
Convert between emoji aliases and their corresponding Unicode characters using the Emoji lookup methods.
```ruby
Emoji.find_by_alias("cat").raw
Emoji.find_by_unicode("\u{1f431}").name
```
--------------------------------
### Edit Existing Emoji with Gemoji
Source: https://context7.com/github/gemoji/llms.txt
Explains how to modify existing emojis using `Emoji.edit_emoji`. This method allows adding new aliases, Unicode mappings, or tags to an emoji. The internal indices are updated to ensure the emoji remains discoverable by its new attributes.
```ruby
require 'gemoji'
# Find and edit an existing emoji
emoji = Emoji.find_by_alias("musical_note")
Emoji.edit_emoji(emoji) do |char|
char.add_alias "music"
char.add_alias "tune"
char.add_unicode_alias "\u{266b}"
char.add_tag "melody"
end
# Now findable by new aliases
puts Emoji.find_by_alias("music").name # Output: musical_note
puts Emoji.find_by_alias("tune").name # Output: musical_note
puts Emoji.find_by_unicode("\u{266b}").name # Output: musical_note
# Add additional tags to smile emoji
smile = Emoji.find_by_alias("smile")
Emoji.edit_emoji(smile) do |char|
char.add_tag "positive"
char.add_tag "friendly"
end
puts smile.tags.include?("positive") # Output: true
```
--------------------------------
### Retrieve and Filter Emojis with Emoji.all
Source: https://context7.com/github/gemoji/llms.txt
Access the master collection of over 1,800 emoji definitions. This method returns an array of Emoji::Character objects, allowing for iteration, category filtering, and metadata extraction.
```ruby
require 'gemoji'
# Get total count of available emojis
emoji_count = Emoji.all.size
puts "Total emojis available: #{emoji_count}"
# Iterate through all emojis in a category
smiley_emojis = Emoji.all.select { |e| e.category == "Smileys & Emotion" }
smiley_emojis.first(5).each do |emoji|
puts "#{emoji.name}: #{emoji.raw} - #{emoji.description}"
end
# Get all unique categories
categories = Emoji.all.map(&:category).uniq.compact
puts categories
```
--------------------------------
### Find Emojis by Alias with Emoji.find_by_alias
Source: https://context7.com/github/gemoji/llms.txt
Look up an emoji using its short text name (e.g., :smile:). Returns the corresponding Emoji::Character object or nil if no match is found.
```ruby
require 'gemoji'
# Find emoji by its alias
emoji = Emoji.find_by_alias('smile')
if emoji
puts "Name: #{emoji.name}"
puts "Raw: #{emoji.raw}"
puts "Category: #{emoji.category}"
puts "Tags: #{emoji.tags.join(', ')}"
puts "Description: #{emoji.description}"
end
# Handle non-existent emoji gracefully
result = Emoji.find_by_alias('nonexistent')
puts result.nil?
```
--------------------------------
### Find Emojis by Unicode with Emoji.find_by_unicode
Source: https://context7.com/github/gemoji/llms.txt
Retrieve an emoji object using its raw Unicode character. This method handles variation selectors and skin tone modifiers, returning the base emoji representation.
```ruby
require 'gemoji'
# Find emoji by Unicode character
emoji = Emoji.find_by_unicode("\u{1f604}")
puts "Found: #{emoji.name}"
# Handles skin tone variants - returns base emoji
wave_dark = Emoji.find_by_unicode("\u{1f44b}\u{1f3ff}")
puts "Base emoji: #{wave_dark.name}"
# Convert Unicode to alias for storage/transmission
def unicode_to_alias(unicode_char)
emoji = Emoji.find_by_unicode(unicode_char)
emoji ? ":#{emoji.name}:" : unicode_char
end
puts unicode_to_alias("😄")
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.