2024/11/12 (็ซ) ่ฟฝ่จ
๐จโ๐ฉโ๐งโ๐ฆ ใจใใ็ตตๆๅญใฏใใฎ่จไบใๆ็จฟใใๆ็นใงใฏ
ใจ่กจ็คบใใใฆใใพใใใใใใ 2024/11/12 (็ซ) ๆ็นใงใฏ็งใฎ macOS (15.0.1) ใฎ Chrome ใงใฏ
ใจ่กจ็คบใใใพใใใใใฏ iOS 17.4 ใฎใชใชใผในๆใซไปฅไธใฎๅคๆดใ่กใใใใใใฎใใใงใใ
ๆนๆณ
ๆธ่จ็ด ใฏใฉในใฟ (grapheme cluster) ใฎๅไฝใงๆฐใใใใใใใใจใๅใใ่ช็ถใ ใจๆใๅไฝใงๆๅญใๆฐใใใใจใใงใใใ
Ruby ใงใฎๆนๆณ
- String#scan ใง
\X
ใๆๅฎใใใ\X
ใฏ Unicode ็ตๅๆๅญใทใผใฑใณในใ่กจใใ - String#grapheme_clusters ใ String#each_grapheme_cluster ใไฝฟใใ
ๆฐใใ
'๐จโ๐ฉโ๐งโ๐ฆ'.length
#=> 7
'๐จโ๐ฉโ๐งโ๐ฆ'.scan(/\X/).length
#=> 1
# String#each_grapheme_cluster ใจ Enumerable#count ใไฝฟใใจ
# ้ไธญใง Array ใชใใธใงใฏใใ็ๆใใใซๆๅญใๆฐใใใใจใใงใใใ
'๐จโ๐ฉโ๐งโ๐ฆ'.each_grapheme_cluster.count
#=> 1
ใซใผใใใ
'๐จโ๐ฉโ๐งโ๐ฆ'.chars
#=> ["๐จ", "โ", "๐ฉ", "โ", "๐ง", "โ", "๐ฆ"]
'๐จโ๐ฉโ๐งโ๐ฆ'.grapheme_clusters
#=> ["๐จโ๐ฉโ๐งโ๐ฆ"]
'๐จโ๐ฉโ๐งโ๐ฆ'.each_char { |chr| puts('U+%04X' % chr.ord) }
# U+1F468
# U+200D
# U+1F469
# U+200D
# U+1F467
# U+200D
# U+1F466
'๐จโ๐ฉโ๐งโ๐ฆ'.each_grapheme_cluster { |chr| puts('U+%04X' % chr.ord) }
# U+1F468