Edited at

[Ruby] ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ ใ‚’ 1 ๆ–‡ๅญ—ใจใฟใชใ™ๆ–นๆณ•


ๆ–นๆณ•

ๆ›ธ่จ˜็ด ใ‚ฏใƒฉใ‚นใ‚ฟ (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(format('U+%04X', chr.ord)) }
# U+1F468
# U+200D
# U+1F469
# U+200D
# U+1F466
# U+200D
# U+1F466

'๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ'.each_grapheme_cluster { |chr| puts(format('U+%04X', chr.ord)) }
# U+1F468


ๅ‚่€ƒ