12
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2019-08-19

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

ๅ‚่€ƒ

12
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?